]*)>/"-6ren">
gpt4 book ai didi

php - 正则表达式查找 % PHP 中的所有子字符串

转载 作者:可可西里 更新时间:2023-11-01 00:57:04 25 4
gpt4 key购买 nike

我想在一个字符串中找到“%”内的所有子串,但我不明白为什么它只找到“id”。

$test = '<img src="%get_love%" alt="%f_id%" title="%id%" />';
$token_regex_inside_tags = "/<([^>]*%([\w]+)%[^>]*)>/";
preg_match_all($token_regex_inside_tags, $test, $matches);

最佳答案

假设:- 我假设您需要在 % 中查找内容只有在 <> 之间.

您可以使用这个使用 \G 的正则表达式

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

Regex Demo

正则表达式分解

(?:
\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 - 正则表达式查找 % PHP 中的所有子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38650793/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com