php求字符串的所有子字符串,正则表达式可找到%PHP中的所有子字符串

我想在字符串的“%”中找到所有子字符串,但是我不明白为什么它只能找到“ id”.

$test = '%f_id%';

$token_regex_inside_tags = "/]*%([\w]+)%[^>]*)>/";

preg_match_all($token_regex_inside_tags, $test, $matches);

解决方法:

假设:-我假设只有在%介于<>之间时,才需要查找%内的内容.

您可以使用此正则表达式使用\G

(?:\G(?!\A)|]*%([^%>]*)%

正则表达式分解

(?:

\G(?!\A) #End of previous match

| #Alternation

< #Match < literally

)

[^%>]* #Find anything that's not % or >

%([^%>]*)% #Find the content within %

在您的正则表达式中

< #Matches < literally

(

[^>]* #Moves till > is found. Here its in end

%([\w]+)% #This part backtracks from last but is just able to find only the last content within two %

[^>]*

)>

标签:php,regex

来源: https://codeday.me/bug/20191026/1939473.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值