dsh7551 2016-04-10 16:03
浏览 19
已采纳

在php中搜索## ... ##中包含的所有字符串

I'm trying to build a program that can read variables from a template and allow the user to edit them. I'm trying to use regex to find instances of user-defined variables in the template, which would look like ##VARIABLENAME##.

I essentially want to grab the VARIABLENAME part into an array item, for each variable that exists in the source string.

I looked around a bit but couldn't find anything, regex syntax is quite a beast to someone who hasn't used it before.

Any help appreciated!

EDIT:

Sample input:

"Hello there ##NAME##, you have ##ORDERSNUM## orders pending".

Expected output:

array[0] = 'NAME'
array[1] = 'ORDERSNUM'

I've so far tried

preg_match("/#(.*)#/", $temphtml, $tempvars) 

But this is only returning the first match found, twice.

  • 写回答

5条回答 默认 最新

  • dongtao4890 2016-04-10 16:20
    关注

    Just use regular expressions like:

    preg_match_all('/##([^#]+)##/', $string, $matches)
    

    see http://php.net/manual/function.preg-match-all.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥30 这个功能用什么软件发合适?
  • ¥60 微信小程序,取消订单,偶尔订单没有改变状态
  • ¥15 用pytorch实现PPO算法
  • ¥15 关于调制信号的星座图?
  • ¥30 前端传参时,后端接收不到参数
  • ¥15 这是有什么问题吗,我检查许可证了但是显示有呢
  • ¥15 机器学习预测遇到的目标函数问题
  • ¥15 Fluent,液体进入旋转区域体积分数不连续
  • ¥15 java linux下将docx文件转pdf
  • ¥15 maven无法下载依赖包