How to count all occurrence of substring in 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";

	$count += substr_count($line, $substring);
}

echo "count: " . $count . "<br>";



/*
run:
 
count: 13642306
 
*/

 



answered Oct 24, 2020 by avibootz

Related questions

1 answer 197 views
1 answer 146 views
2 answers 195 views
1 answer 198 views
1 answer 195 views
...