note 44708 added to function.strstr
| From: | logicoATavantel dot net at osu1 dot php dot net | Date: | Fri, 13 Aug 2004 22:05:10 +0000 |
| Subject: | note 44708 added to function.strstr | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-74582@lists.php.net to get a copy of this message | ||
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");
----
Manual Page -- http://www.php.net/manual/en/function.strstr.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+44708
Delete -- http://master.php.net/manage/user-notes.php?action=delete+44708&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+44708&report=yes
Search -- http://master.php.net/manage/user-notes.php