How to count all occurrences of a word in a text file with PHP

1 Answer

0 votes
$f = fopen("e:/datefile.txt", "r") or die("Error open file!");
$s = "";
$word = "web";
$count = 0;
while(!feof($f)) 
{
    $s = fgets($f);
    //  stripos() function is NOT case-sensitive it's case-insensitive
    if (stripos($s, $word) !== false)
        $count++;
}
fclose($f);
echo $count;
    

/*
run:

2

*/

 



answered Dec 5, 2015 by avibootz
edited Dec 5, 2015 by avibootz

Related questions

1 answer 94 views
1 answer 174 views
1 answer 235 views
1 answer 185 views
1 answer 178 views
1 answer 175 views
...