doubingling4706 2015-12-11 00:50
浏览 7
已采纳

搜索所有[{my value here}]并将它们放在PHP中的数组中

I have the following HTML code below, which acts as a template for a module on my php application. The following HTML code is stored as the string:

$parsedTemplate

Here is the HTML

<td>
    [{item3}]
</td>

<td>
    [{item2}]
</td>

<td>
    [{item3}]
</td>

I want php to search the $parsedTemplate string and make an array like the following

'item1','item2','item3'

How can I achieve this in an efficient way? Regex maybe?

  • 写回答

1条回答 默认 最新

  • douna3367 2015-12-11 01:11
    关注

    Try this regex:

    (?s)\[{\K.*?(?=}])
    

    Regex live here.

    Explaining:

    (?s)      # allows . to match new line
    \[{       # your open-delimiter
    \K        # you do not need the delimiter, then clear it
    .*?       # till the first-next occurrence where
    (?=}])    # is possible to see the close-delimiter
    

    Hope it helps.


    Edit: To test, it would be like:

    preg_match_all('/(?s)\[{\K.*?(?=}])/', $parsedTemplate, $match);
    print_r($match);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度