grep
(如果您使用的是 Linux 机器)
<?php
// In addition to escaping, the input should be validated and santitized
$userInput = escapeshellarg('text');
// Run the command on all the files (* = all files)
// -n include line numbers in the result, -I ignore binary
$result = trim(`grep -In $userInput *`);
// Break the result into an array by lines
$lines = explode(PHP_EOL,$result);
foreach ($lines as $l) {
// The first output is the filename, followed by the line number
$firstColon = strpos($l, ':');
if ($firstColon !== false) {
$filename =substr($l,0,$firstColon);
$link = <<<LINK
<a href="$filename">$l</a>
LINK;
echo $link.PHP_EOL;
}
}