note 96525 added to function.strpos
| From: | http://restaurants dot nl at osu1 dot php dot net | Date: | Wed, 03 Mar 2010 16:50:51 +0000 |
| Subject: | note 96525 added to function.strpos | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-166581@lists.php.net to get a copy of this message | ||
I've seen some awful and even outright bizarre examples for finding the nth occurrence of
needle inside haystack starting at a given offset,
so here's what I believe is a better one:
function strnpos($haystack, $needle, $nth, $offset = 0)
{
for ($retOffs=$offset-1; ($nth>0)&&($retOffs!==FALSE); $nth--) $retOffs =
strpos($haystack, $needle, $retOffs+1);
return $retOffs;
}
echo strnpos("who's your daddy, daddy?", "d", 1); // 11
echo strnpos("who's your daddy, daddy?", "d", 3); // 14
echo strnpos("who's your daddy, daddy?", "d", 2, 14); // 18
echo strnpos("who's your daddy, daddy?", "daddy", 2); // 18
enjoy
----
Server IP: 83.137.20.135
Probable Submitter: 195.240.33.10
----
Manual Page -- http://www.php.net/manual/en/function.strpos.php
Edit -- https://master.php.net/note/edit/96525
Del: integrated -- https://master.php.net/note/delete/96525/integrated
Del: useless -- https://master.php.net/note/delete/96525/useless
Del: bad code -- https://master.php.net/note/delete/96525/bad+code
Del: spam -- https://master.php.net/note/delete/96525/spam
Del: non-english -- https://master.php.net/note/delete/96525/non-english
Del: in docs -- https://master.php.net/note/delete/96525/in+docs
Del: other reasons-- https://master.php.net/note/delete/96525
Reject -- https://master.php.net/note/reject/96525
Search -- https://master.php.net/manage/user-notes.php