简体   繁体   English

如何使用正则表达式查找特定模式?

[英]How to find particular pattern using regex?

I have some.php files in a directory that calls a user defined function:我在调用用户定义的 function 的目录中有一些.php 文件:

_l($string);

each time different string is passed to that function and all strings are static, (ie not a single string is entered by user input).每次将不同的字符串传递给该 function 并且所有字符串都是 static,(即用户输入没有输入单个字符串)。

Now I want a script that can list down all the strings form all the files of that directory, which are passed into _l($string);现在我想要一个脚本,它可以列出该目录中所有文件的所有字符串,这些文件被传递到 _l($string); I had tried:我试过:

 $fp = fopen($file, "r");
    while(!feof($fp)) {
      $content .= fgets($fp, filesize($file));
      if(preg_match_all('/(_l\(\'.*?\'\);)/', fgets($fp, filesize($file)), $matches)){
         foreach ($matches as $key => $value) {
           array_push($text, $value[0]);
         }
      }
   }

I get strings but not every strings those are in files, some strings are not match with given regex, so what are the condiotion that is required to get all the strings?我得到字符串,但不是文件中的每个字符串,有些字符串与给定的正则表达式不匹配,那么获取所有字符串所需的条件是什么?

This is easier to get strings in double " or single ' quotes as the _l() function argument.这更容易获得双"或单'引号中的字符串作为_l() function 参数。

$string = file_get_contents($file);
preg_match_all('/_l\([\'"](.*?)[\'"]\);/', $string, $matches);
$text = $matches[1];

If needed you can add some optional spaces before and after the ( and before the ) :如果需要,您可以在()之前和之后添加一些可选空格:

'/_l\s*\(\s*[\'"](.*?)[\'"]\s*\);/'

Also, if the function can be used in a loop or if or something where it's not terminated by a semi-colon ;此外,如果 function 可以在循环中使用,或者if或其他不以分号结尾的东西; then remove it from the pattern.然后将其从图案中移除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM