Find the string and then the line number using php from text file

Click to rate this tutorial!
[Total: 3 Average: 5]

Find the string and then the line number using php from text file or a text data base.
In this we are searching a string named alan inside one of our log files.

$searchString = 'alan';
$URL = "logs/logs.txt_2011-08-10";
$data = file_get_contents($URL);
$data = explode("\n", $data);
for ($line = 0; $line < count($data); $line++) {
if (strpos($data&#91;$line&#93;,$searchString) != 0) {
echo "String found on line ".++$line." with string $searchString<br>";
--$line;
}
}
Click to rate this tutorial!
[Total: 3 Average: 5]

Leave a Reply

Your email address will not be published.