note 9472 modified in function.substr-count by irchtml
| From: | irchtml@php.net | Date: | Tue, 02 Mar 2004 11:21:00 +0000 |
| Subject: | note 9472 modified in function.substr-count by irchtml | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-66087@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;
?>
--was--
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/en/function.substr-count.php