Click to See Complete Forum and Search --> : Counting apperence of word in TXT file?


Extreme
10-16-2003, 07:46 PM
Anyone know a way to make script, that will load a big TXT file and count how many times some word appears in it? Example, I wish to count how many "Name:" words are there in in this file I loaded..

pyro
10-16-2003, 09:17 PM
Try something like this:

<?PHP
$str = "This is a test string which will contain test characters. Let's run a test, and then be done.";
preg_match_all("/\btest\b/", $str, $num);
$num = count($num[0]);
echo $num
?>

Extreme
10-17-2003, 04:23 PM
I don't see code to browse for my text file?? I have to load it into script somehow..

pyro
10-17-2003, 04:39 PM
I just posted the script as an example, not a full script...


<?PHP
$contents = file("yourfile.txt");
$lines = "";
foreach ($contents as $line) {
$lines .= $line;
}
preg_match_all("/btestb/", $lines, $num);
$num = count($num[0]);
echo $num
?>