note 44708 deleted from function.strstr by tomsommer
| From: | tomsommer@php.net | Date: | Fri, 13 Aug 2004 22:12:34 +0000 |
| Subject: | note 44708 deleted from function.strstr by tomsommer | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-74583@lists.php.net to get a copy of this message | ||
Note Submitter: logico AT avantel . net
----
If you want a recursive function that counts the amount of occurrences of a string within another:
function occurrenceCount($word, $string, $case_sensitive = true, $actual_count = 0){
if(!$case_sensitive){
$as = strtolower($string); // Actual string
$aw = strtolower($word); // Actual word
} else {
$as = $string; // Actual string
$aw = $word;
}
$as = strstr($as, $word);
if(!$as){
return $actual_count;
} else {
$as = substr($as, strlen($aw), strlen($as)); // Let's remove the searched word from
the beginning of the string
return occurrenceCount($aw, $as, $case_sensitive, ++$actual_count);
}
}
Just call it:
echo "Ocurrences of 'dog' within 'eat your own dog food' is:
".ocurrenceCount("dog", "eat your own dog food");