note 9472 deleted from function.substr-count by nlopess

From: Date: Mon, 07 Jun 2004 09:36:25 +0000
Subject: note 9472 deleted from function.substr-count by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-70474@lists.php.net to get a copy of this message
Note Submitter: amir@browse.co.uk ---- 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; ?>

« previous php.notes (#70474) next »