note 108425 added to function.stripos
| From: | mtroy dot student at gmail dot com | Date: | Wed, 25 Apr 2012 16:42:23 +0000 |
| Subject: | note 108425 added to function.stripos | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-187340@lists.php.net to get a copy of this message | ||
when you want to know how much of substring occurrences, you'll use "substr_count".
But, retrieve their positions, will be harder.
So, you can do it by starting with the last occurrence :
function stripos_r($haystack, $needle)
{
if(strlen($needle) > strlen($haystack))
trigger_error(sprintf("%s: length of argument 2 must be <= argument 1",
__FUNCTION__), E_USER_WARNING);
$seeks = array();
while($seek = strripos($haystack, $needle))
{
array_push($seeks, $seek);
$haystack = substr($haystack, 0, $seek);
}
return $seeks;
}
it will return an array of all occurrences a the substring in the string
Example :
$test = "this is a test for testing a test function... blah blah";
var_dump(stripos_r($test, "test"));
// output
array(3) {
[0]=>
int(29)
[1]=>
int(19)
[2]=>
int(10)
}
Paul-antoine.
----
Server IP: 89.31.146.176
Probable Submitter: 82.236.25.214
----
Manual Page -- http://www.php.net/manual/en/function.stripos.php
Edit -- https://master.php.net/note/edit/108425
Del: integrated -- https://master.php.net/note/delete/108425/integrated
Del: useless -- https://master.php.net/note/delete/108425/useless
Del: bad code -- https://master.php.net/note/delete/108425/bad+code
Del: spam -- https://master.php.net/note/delete/108425/spam
Del: non-english -- https://master.php.net/note/delete/108425/non-english
Del: in docs -- https://master.php.net/note/delete/108425/in+docs
Del: other reasons-- https://master.php.net/note/delete/108425
Reject -- https://master.php.net/note/reject/108425
Search -- https://master.php.net/manage/user-notes.php