minnemike14 Posted August 7, 2014 Share Posted August 7, 2014 Hello - hoping to find some direction with this. I think this is fairly simple for more experienced php coders but I'm having trouble with it. It's a php grep text string search that results in a list of files that contain the string. Hoping to split the $results value (list of file names) into separate links to open the file. I was able to convert into a link for the first file listed, but it links to that same file for all the entries in the list. I assume that maybe an array of $results needs to be created first rather than try and work with $results at the end? <?php /** * E.Yekta * cafewebmaster.com */ define("SLASH", stristr($_SERVER[SERVER_SOFTWARE], "win") ? "\\" : "/"); $path = ($_POST[path]) ? $_POST[path] : dirname(__FILE__) ; $q = $_POST[q]; function php_grep($q, $path){ $fp = opendir($path); while($f = readdir($fp)){ if( preg_match("#^\.+$#", $f) ) continue; // ignore symbolic links $file_full_path = $path.SLASH.$f; if(is_dir($file_full_path)) { $ret .= php_grep($q, $file_full_path); } else if( stristr(file_get_contents($file_full_path), $q) ) { $ret .= "$file_full_path\n"; } } return $ret; } if($q){ $results = php_grep($q, $path); } echo <<<HRD <pre > <form method=post> <input name=path size=100 value="$path" /> Path <input name=q size=100 value="$q" /> Query <input type=submit> </form> $results </pre > HRD; ?> php_grep.php Quote Link to comment https://forums.phpfreaks.com/topic/290338-help-with-php-grep-search-splitting-results-into-ind-linksurls/ Share on other sites More sharing options...
Jacques1 Posted August 7, 2014 Share Posted August 7, 2014 Throwing all search results into one big string is indeed a poor choice. This may make sense for the shell of an operating system, but it makes absolutely no sense for a programming language. So, yes, do return an array. Besides that, why do you use stristr()? This gives you the entire substring from the first occurence of the search term. But you just want to know whether the search term exists within the string. That's what stripos() is for. Quote Link to comment https://forums.phpfreaks.com/topic/290338-help-with-php-grep-search-splitting-results-into-ind-linksurls/#findComment-1487138 Share on other sites More sharing options...
minnemike14 Posted August 7, 2014 Author Share Posted August 7, 2014 Thanks for your response... how would you go about creating an array before it compiles $results? At which point in the script? Example? Thank you again! Quote Link to comment https://forums.phpfreaks.com/topic/290338-help-with-php-grep-search-splitting-results-into-ind-linksurls/#findComment-1487142 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.