Dysan Posted April 23, 2008 Share Posted April 23, 2008 Hi, Using PHP, how do I search through all .php files contained in a folder (set by user) for a word (also set by user)? Upon finding occurrences of the specified word, how do I display the line number and file path it is in? Link to comment https://forums.phpfreaks.com/topic/102524-search-php-file-for-word/ Share on other sites More sharing options...
The Little Guy Posted April 23, 2008 Share Posted April 23, 2008 first I would glob() all the php files: This is just an example (not the real deal): <?php $find = '~'.$_POST['searchString'].'~'; foreach(glob($_POST['selectedDirectory'].'/*.php') as $filename){ // * stands for wild card $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); fclose($handle); if(preg_match($find,$contents,$matches)){ echo 'Found a match: '.$matches[0].' in file: '.$filename; } } ?> Link to comment https://forums.phpfreaks.com/topic/102524-search-php-file-for-word/#findComment-524975 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.