How to count all occurrence of substring in each line of a text file with PHP

1 Answer

0 votes
$file_name = "data.txt";

$file = fopen($file_name, "r");

$count = 0;
while (!feof($file)) {
	$line = trim(fgets($file));

	$substring = "url:";

	if (strpos($line, $substring) != false) {
		$count++;
	}
}
echo "count: " . $count . "<br>";



/*
run:

count: 13342281

*/

 



answered Oct 24, 2020 by avibootz

Related questions

1 answer 148 views
1 answer 210 views
1 answer 178 views
1 answer 174 views
1 answer 219 views
...