Skip to content

Instantly share code, notes, and snippets.

@billcreswell
Last active March 1, 2016 00:44
Show Gist options
  • Save billcreswell/10533765 to your computer and use it in GitHub Desktop.
Save billcreswell/10533765 to your computer and use it in GitHub Desktop.
http://stackoverflow.com/questions/19971428/recursively-search-all-directories-for-an-array-of-strings-in-php?lq=1
$contents_list = array("xyz","abc","hello");
$path = "/tmp/";
$pattern = implode('\|', $contents_list) ;
$command = "grep -r '$pattern' $path";
$output = array();
exec($command, $output);
foreach ($output as $match) {
echo $match . '\n';
}
function recursiveDirList($dir, $prefix = '') {
$dir = rtrim($dir, '/');
$result = array();
foreach (glob("$dir/*", GLOB_MARK) as &$f) {
if (substr($f, -1) === '/') {
$result = array_merge($result, recursiveDirList($f, $prefix . basename($f) . '/'));
} else {
$result[] = $prefix . basename($f);
}
}
return $result;
}
$files = recursiveDirList($path);
foreach ($files as $filename) {
$file_content = file($path . '/' . $filename);
foreach ($file_content as $line) {
foreach($contents_list as $content) {
if(strpos($line, $content) !== false) {
echo $line . '\n';
}
}
}
}
http://stackoverflow.com/questions/8032312/find-specific-text-in-multiple-txt-files-in-php?rq=1
$path_to_check = '';
$needle = 'match';
foreach(glob($path_to_check.'*.txt') as $filename)
{
foreach(file($filename) as $fli=>$fl)
{
if(strpos($fl, $needle)!==false)
{
echo $filename.' on line '.($fli+1).': '.$fl;
}
}
}
$search_pattern = "text to find";
$output = array();
$result = exec("/path/to/grep -l " . escapeshellarg($search_pattern) . " /path/to/directory/*", $output);
print_r($output);
<?php
$dir = '/tmp';
$files = scandir($dir);
print_r($files);
?>
<?php
function rsearch($folder, $pattern) {
$dir = new RecursiveDirectoryIterator($folder);
$ite = new RecursiveIteratorIterator($dir);
$files = new RegexIterator($ite, $pattern, RegexIterator::GET_MATCH);
$fileList = array();
foreach($files as $file) {
$fileList = array_merge($fileList, $file);
}
return $fileList;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment