<?php
$file = 'c:\test.txt';
$fh = fopen($file,'r');
$data = fread($fh,filesize($file));
fclose($fh);
// $subject = file_get_contents($data);
$pattern = '/(test)\b.*/is';
preg_match($pattern, $data, $matches);
$count = count($matches);
echo $count;
print_r($matches);
?>
Output:
C:>c:\php\cli\php.exe -f c:\1.php
2Array
(
[0] => test
test
test
test
12345
12345
12345
12345
[1] => test
)
C:>
the variable $count seems to be just echoing the number of array elements and not how many times the word test appears in the string.
the test.txt document is formatted as follows:
test
test
test
test
12345
12345
12345
12345
I had to echo out the $subject line as it doesnt like my input data 🙂