PHP File Search within file
From w3cyberlearnings
Contents |
PHP Search within a file
Search within a file to look for a specific word.
File Content(test1.txt)
a good person make a good world and nothing is better than them. a good person loves to make peace. a good person mostly don't make a lot of problem. a good person likes to say good word. nothing is a bad person unless she or he loves to say bad. forget to tell you how a good person loves peace.
Example 1
<?php $searchword = "nothing"; $matches = array(); $handle = fopen("test1.txt", "r"); if ($handle) { while (!feof($handle)) { $buffer = fgets($handle); if (strpos($buffer, $searchword) !== FALSE) $matches[] = $buffer; } fclose($handle); } echo 'I found :<br/>'; echo join('<br/>',$matches); ?>
Output
I found : a good person make a good world and nothing is better than them. nothing is a bad person unless she or he loves to say bad.