How to find a word in a text file in PHP

2 Answers

0 votes
$data = file_get_contents("d:\\test.txt");

if (stripos($data, "programming") !== false) // case-insensitive search  stripos()
    echo 'Found' . "
\n"; else echo 'NOT Found' . "
\n"; /* run: Found */


answered Nov 1, 2014 by avibootz
0 votes
$f = fopen("e:/datefile.txt", "r") or die("Error open file!");
$s = "";
$word = "Applications";
$found = 0;

while(!feof($f)) {
    $s = fgets($f);
    if (strpos($s, $word) !== false) {
        $found = 1;
        break;
    }
}

fclose($f);

echo "The word: " . $word;
if ($found == 1)
    echo " exist";
else
    echo " NOT exist";
echo " in text file: datefile.txt"
     
  
 
/*
run
 
The word: Applications exist in text file: datefile.txt
 
*/

 



answered Apr 14, 2024 by avibootz

Related questions

1 answer 172 views
1 answer 199 views
1 answer 225 views
1 answer 221 views
1 answer 190 views
1 answer 183 views
...