php preg_match_all 个函数参数在 php 个函数的文件中

php preg_match_all function parameters in php file of one function

我正在尝试通过以下文本查找 functionName('MATCH_1') ;functionName( "MATCH2"); 和... functionName ('MATCH_X') ; 并提取 MATCH_XXX

正文: 一些文本 functionName('MATCH_1');和其他文本 functionName( "MATCH_2") ;和许多其他文本。

或下一行中的更多 functionName( 'MATCH_X'); ... 更多文字。

我想得到 MATCH_1MATCH2MATCH_X

我的尝试:

<?php
preg_match_all('#functionName\s*\(\s*(\'|")(.*)(\'|")\s*\)\s*;#im', $content,$matches);

但是我的 RegEx 不符合我的要求。 你能解释一下吗,因为 RegEx 必须看起来像。以及他为什么那样工作。

问候 raiserle

试试这个

$content = "The text: Some text functionName('MATCH_1'); and other text functionName( \"MATCH_2\") ; and many another text. Or many more in next line functionName( 'MATCH_X'); ... more text.";

preg_match_all('#functionName\s*\(\s*(\'|")(.*?)(\'|")\s*\)\s*;#im', $content,$matches);

var_dump($matches);

然后你应该在 $matches[2] 中得到你想要的东西。

我改变的是我在正则表达式的 .* 部分之后添加了 ?,使其成为 non-greedy(匹配最短的字符串而不是最长的匹配)。这确实降低了性能。请参阅:here 了解原因和更详细的解释。