Ho trovato in giro per la rete queste righe di codice ho possono essere utili per quello che devo fare.
Però avrei bisogno di modificarle in modo tale che vengano fornite tutte le ricorrenze all'interno dei file e non si interrompa la ricerca dopo la prima stringa trovata in ogni file.
Poi ho bisogno di visualizzare non il nome del file ma delle stringhe associate al nome del file.
Esempio se il file che contiene la stringa si chiama pippo e suppongo di aver associato la stringa pluto al file di nome pippo. Quando viene trovata la stringa deve essere visualizzato pluto e non pippo.
Come posso modificare questo codice?
<?php
$searchstr = ( isSet( $_POST["searchstr"] ) ? $_POST["searchstr"] : "" );
?>
<html>
<head><title>Site Grep Search-engine</title></head>
<body>
<form action="<?=$PHP_SELF;?>" method="post">
<input type="text" name="searchstr" value="<?php echo "$searchstr"; ?>" size="20" maxlength="30"/>
<input type="submit" value="Search!"/>
</form>
</p>
<?php
if ( ! empty( $searchstr ) ) {
// empty() is used to check if we've any search string
// if we do, call grep and display the results.
echo "<hr/>\n";
// call grep with case-insensitive search mode on all files
$cmdstr = "grep -i $searchstr *";
$fp = popen( $cmdstr, "r" ); // open the output of command as a pipe
$myresult = array(); // to hold my search results
while( $buffer = fgetss ( $fp, 4096 ) ) {
// grep returns in the format
// filename: line
// So, we use split() to split the data
list( $fname, $fline ) = split( ":", $buffer, 2 );
// we take only the first hit per file
if ( !defined( $myresult[$fname] ) )
$myresult[$fname] = $fline;
}
// we have results in a hash. lets walk through it & print it
if ( count( $myresult ) ) {
echo "\n";
- \n";
while( list( $fname, $fline ) = each( $myresult ) )
echo "- <a href=\"$fname\">$fname</a> : $fline \n";
echo "
} else {
// no hits
echo "Sorry. Search on $searchstr returned no results.
\n";
}
pclose( $fp );
}
?>
</body>
</html>