note 107678 added to function.strpos
| From: | martijn at martijnfrazer dot nl | Date: | Sat, 25 Feb 2012 15:58:22 +0000 |
| Subject: | note 107678 added to function.strpos | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-186361@lists.php.net to get a copy of this message | ||
This is a function I wrote to find all occurrences of a string, using strpos recursively.
<?php
function strpos_recursive($haystack, $needle, $offset = 0, &$results = array()) {
$offset = strpos($haystack, $needle, $offset);
if($offset === false) {
return $results;
} else {
$results[] = $offset;
return strpos_recursive($haystack, $needle, ($offset + 1), $results);
}
}
?>
This is how you use it:
<?php
$string = 'This is some string';
$search = 'a';
$found = strpos_recursive($string, $search);
if($found) {
foreach($found as $pos) {
echo 'Found "'.$search.'" in string "'.$string.'" at
position <b>'.$pos.'</b><br />';
}
} else {
echo '"'.$search.'" not found in "'.$string.'"';
}
?>
----
Server IP: 213.249.64.162
Probable Submitter: 82.215.50.52
----
Manual Page -- http://www.php.net/manual/en/function.strpos.php
Edit -- https://master.php.net/note/edit/107678
Del: integrated -- https://master.php.net/note/delete/107678/integrated
Del: useless -- https://master.php.net/note/delete/107678/useless
Del: bad code -- https://master.php.net/note/delete/107678/bad+code
Del: spam -- https://master.php.net/note/delete/107678/spam
Del: non-english -- https://master.php.net/note/delete/107678/non-english
Del: in docs -- https://master.php.net/note/delete/107678/in+docs
Del: other reasons-- https://master.php.net/note/delete/107678
Reject -- https://master.php.net/note/reject/107678
Search -- https://master.php.net/manage/user-notes.php