我有一个字符串,例如:
$foo = 'Hello __("How are you") I am __("very good thank you")'
我知道这是一个奇怪的字符串,但请留在我身边:P
我需要一个正则表达式来查找 __("在此处查找内容") 之间的内容
并将其放入数组中。
即正则表达式会找到“你好吗”和“非常好,谢谢”。
尝试这个:
preg_match_all('/(?<=__\(").*?(?="\))/s', $foo, $matches);
print_r($matches);
意思是:
(?<= # start positive look behind
__\(" # match the characters '__("'
) # end positive look behind
.*? # match any character and repeat it zero or more times, reluctantly
(?= # start positive look ahead
"\) # match the characters '")'
) # end positive look ahead
EDIT
正如格雷格提到的:对于不太熟悉环视的人来说,将它们排除在外可能会更具可读性。然后你匹配所有内容:__("
, the string and ")
并包装匹配的正则表达式string, .*?
,在括号内仅捕获那些字符。然后你需要得到你的比赛$matches[1]
。演示:
preg_match_all('/__\("(.*?)"\)/', $foo, $matches);
print_r($matches[1]);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)