1

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?

9
  • 1
    It may be because you have 2 calls to fgets(), try removing the one in the preg_match_all call and use $content instead. Commented Feb 5, 2021 at 6:13
  • tried, but this time all the strings disappeared which were fetched by not removing fgets(), only 1st string fetched. Commented Feb 5, 2021 at 6:31
  • Can you show an example where the strings are missed. Commented Feb 5, 2021 at 6:33
  • _l($string) as pattern in preg_match_all() is not called as function but rather as regex string ? Commented Feb 5, 2021 at 7:30
  • @jacouh yes, it will be consider as a pattern Commented Feb 5, 2021 at 7:57

1 Answer 1

1

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

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.