search for string in php files

Rahul Sehrawat
Rahul Sehrawat used Ask the Experts™
on
Hi,

I am looking for php function which would search for a string(s) in all the PHP files in a specific directory (including subdirectories). If possible, If I can exclude the specific folder while searching. I would want the list of all files using specified string(s).

Any help / lead will be appreciated.

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Does this have to be part of an application or is it separate? Will you need to repeat this process?
There are many tools for searching through files for specific strings or patterns - they differ based on the use-case.

Author

Commented:
I am actually creating a application. So it will be part of that. and yes, this process will be repeated time to time.

Thanks
Olaf DoschkeSoftware Developer

Commented:
Take a look at http://php.net/manual/de/class.directoryiterator.php 
Once you have a list of files you can iterate them and use http://php.net/manual/de/function.file-get-contents.php to search them for a term.

By the way, many editors have that functionality built in already.

Bye, Olaf.
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
You could do something like this
<?php
// FOLDER TO SEARCH IN
$target_folder  = "./";
// TEXT TO SEARCH FOR
$searchtext = 'in_array';

// CALL SEARCH FUNCTION
$res = doSearch($target_folder, $searchtext);

// DUMP RESULTS
echo "<pre>" . print_r($res, true) . "</pre>";


// RECURSIVE SEARCH FUNCTION
function doSearch($target, $searchtext) {
  $results = array();
  if (is_dir($target)) {
    if ($dh = opendir($target)) {      
      while(($file = readdir($dh)) != false) {
        if ($file == '.' || $file == '..') continue;
        if (is_dir($file)) {
          $subdir = "{$target}{$file}/";
          $results = array_merge($results, doSearch($subdir, $searchtext));
        }
        else {
          $ext = strtolower(end(explode('.', $file)));
          if ( $ext == 'php') {
            $path = $target . $file;
            if( strpos(file_get_contents($path),$searchtext) !== false) {
              $results[] = $path;
            }
          }
        }
      }
      closedir($dh);
    }
  }
  return $results;
}

Open in new window

Author

Commented:
Hi Julian,

Thanks for the function. I am only facing one issues with the function that when I set target as "../" it doesn't work.

Thanks
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
Two small changes are needed
1. Prepend the $target path to the file when do an is_dir check
2. Add set_time_limit to the loop to prevent script from timing out if operation takes longer than timeout limit (default 30 sec)

Updated code below
<?php
// FOLDER TO SEARCH IN
$target_folder  = "../";
// TEXT TO SEARCH FOR
$searchtext = 'in_array';

// CALL SEARCH FUNCTION
$res = doSearch($target_folder, $searchtext);

// DUMP RESULTS
echo "<pre>" . print_r($res, true) . "</pre>";


// RECURSIVE SEARCH FUNCTION
function doSearch($target, $searchtext) {
  set_time_limit(120);
  $results = array();
  if (is_dir($target)) {
    if ($dh = opendir($target)) {      
      while(($file = readdir($dh)) != false) {
        if ($file == '.' || $file == '..') continue;
        if (is_dir($target . $file)) {
          $subdir = "{$target}{$file}/";
          $results = array_merge($results, doSearch($subdir, $searchtext));
        }
        else {
          $ext = strtolower(end(explode('.', $file)));
          if ( $ext == 'php') {
            $path = $target . $file;
            if( strpos(file_get_contents($path),$searchtext) !== false) {
              $results[] = $path;
            }
          }
        }
      }
      closedir($dh);
    }
  }
  return $results;
}

Open in new window

Author

Commented:
Awesome. Works like a charm. :)
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
You are welcome.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial