douxianglu4370 2014-10-07 20:30
浏览 37
已采纳

在字符串模板中查找文本PHP [关闭]

I have a series of string templates, for example:

Confirmation Number: 1234
Other random number: 1231
Order Name: Something

What I'm trying to do is getting these numbers and names from the template and use them as variables... I know it's very simple, but how would I go about doing this?

Cheers!

EDIT: The best approach for me to do this would be entering something like:

Confirmation Number: {CONFIRMATION_NUMBER}
Other random number: {OTHER_NUMBER}
Order Name: {ORDER_NAME}

And then get whatever's in the brackets from the template.

  • 写回答

1条回答 默认 最新

  • dth34653 2014-10-07 20:37
    关注

    Hi something like this might do the trick.

    preg_match('/^(?P<key>.+)\:\s*(?P<value>[\w]+)$/', $line, $match);
    

    then use $match['key'] and $match['value']

    demo http://regex101.com/r/eN1rW0/2

    You might need a space in the value?

    preg_match('/^(?P<key>.+)\:\s*(?P<value>[\w\s]+)$/', $line, $match);
    

    Or anything in the value?

    preg_match('/^(?P<key>.+)\:\s*(?P<value>.+)$/', $line, $match);
    

    Demo

    http://regex101.com/r/eN1rW0/3

    To capture the : you just want to move that inside of the ( capture group ) like so

        preg_match('/^(?P<key>.+\:)\s*(?P<value>.+)$/', $line, $match);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题