note 9472 added to substr_count

From: Date: Mon, 06 Nov 2000 00:38:03 +0000
Subject: note 9472 added to substr_count
Groups: php.notes 
Request: Send a blank email to php-notes+get-7412@lists.php.net to get a copy of this message
I wanted to count the total number of string occurances within a text file, using PHP3. I was looking for the total occurances of two strings, 'a1' and 'a2'. Here's how I did it: <?php // Open the text file $filename = "../log.txt"; $fp = fopen($filename,"r"); // Read the file $haystack = fread($fp,filesize($filename)); // Define search $needle1 = "a1"; $needle2 = "a2"; // Count the number of occurences $num_occurs1 = count(explode($needle1,$haystack)) - 1; $num_occurs2 = count(explode($needle2,$haystack)) - 1; // Set the total number of occurences $total1 = $num_occurs1; $total2 = $num_occurs2; ?> http://www.php.net/manual/function.substr-count.php

« previous php.notes (#7412) next »