Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
156 views
in Technique[技术] by (71.8m points)

php - How to find particular pattern using regex?

I have some .php files in a directory that calls a user defined function:

_l($string);

each time different string is passed to that function and all strings are static, (i.e. not a single string is entered by user input).

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); 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?

question from:https://stackoverflow.com/questions/66058013/how-to-find-particular-pattern-using-regex

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is easier to get strings in double " or single ' quotes as the _l() function argument.

$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 ):

'/_ls*(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 ; then remove it from the pattern.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...