search for string in php files

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
Rahul SehrawatAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
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.
0
Rahul SehrawatAuthor 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
0
Olaf DoschkeSoftware DeveloperCommented:
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.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Julian HansenCommented:
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

0
Rahul SehrawatAuthor 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
0
Julian HansenCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rahul SehrawatAuthor Commented:
Awesome. Works like a charm. :)
0
Julian HansenCommented:
You are welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.