Page 1 of 1

Going through array

Posted: Sat Jan 29, 2011 2:06 pm
by jbriggsbloni
I have an array of strings and a text file. I'm trying to see how many times each of the strings are in the text file (in .txt format). What is the best way to do this? Thanks for any help

Re: Going through array

Posted: Sat Jan 29, 2011 5:31 pm
by spedula
I would open the file with fopen() and save the contents of is as $string. Then use substr_count() on that string.

Code: Select all

$filename = "something.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);

for($i = 0; $i < count($array); $i++) {
   echo $array[i]." occurs: ".substr_count($contents, $array[$i])." times in the file./n";
}
I haven't tested it but it should work or something very similar.