/
search-code.php
48 lines (40 loc) · 1.1 KB
/
search-code.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/php
<?php
if(empty($argv[1])) {
error('Missing file type');
}
if(empty($argv[2])) {
error('Missing string to search');
}
$path = $argv[1];
$string = $argv[2];
$files = globRecursive($path, '.');
foreach($files as $file) {
$fileContents = file_get_contents($file);
$fileContentsArray = explode("\n", $fileContents);
foreach($fileContentsArray as $lineNumber => $row) {
if(strpos($row, $string) !== false) {
$lineNumber++;
$row = trim($row);
echo "$file:$lineNumber\n\t\t\t$row\n\n";
}
}
}
function error($msg = '') {
echo "\n" . ($msg ?: 'Error') . "\n\n";
die();
}
function globRecursive($find, $path = '.') {
$dh = opendir($path);
$found = glob("$path/$find");
while(($file = readdir($dh)) !== false) {
if($file === '..') continue;
if($file === '.') {
$found = array_merge($found, glob("$path/$find"));
} elseif(is_dir("$path/$file")) {
$found = array_merge($found, globRecursive($find, "$path/$file"));
}
}
closedir($dh);
return $found;
}