note 68073 added to function.strpos
| From: | chasesan at gmail dot com | Date: | Wed, 12 Jul 2006 10:48:24 +0000 |
| Subject: | note 68073 added to function.strpos | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-114707@lists.php.net to get a copy of this message | ||
You can use strpos to produce a funciton that will find the nth instance of a certain string within
a string. Personally I find this function almost more useful then strpos itself.
I kinda wish they would put it stock into php but I doupt thats gonna happen any time soon. ^_^
Here is da code:
<?php
//just like strpos, but it returns the position of the nth instance of the needle (yay!)
function strpos2($haystack, $needle, $nth = 1)
{
//Fixes a null return if the position is at the beginning of input
//It also changes all input to that of a string ^.~
$haystack = ' '.$haystack;
if (!strpos($haystack, $needle))
return false;
$offset=0;
for($i = 1; $i < $nth; $i++)
$offset = strpos($haystack, $needle, $offset) + 1;
return strpos($haystack, $needle, $offset) - 1;
}
?>
----
Server IP: 216.194.113.175
Probable Submitter: 69.95.70.124
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.strpos.php
Edit -- http://master.php.net/note/edit/68073
Del: integrated -- http://master.php.net/note/delete/68073/integrated
Del: useless -- http://master.php.net/note/delete/68073/useless
Del: bad code -- http://master.php.net/note/delete/68073/bad+code
Del: spam -- http://master.php.net/note/delete/68073/spam
Del: non-english -- http://master.php.net/note/delete/68073/non-english
Del: in docs -- http://master.php.net/note/delete/68073/in+docs
Del: other reasons-- http://master.php.net/note/delete/68073
Reject -- http://master.php.net/note/reject/68073
Search -- http://master.php.net/manage/user-notes.php