note 83437 added to function.strpos
| From: | pawhox at osu1 dot php dot net | Date: | Mon, 26 May 2008 13:19:54 +0000 |
| Subject: | note 83437 added to function.strpos | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-139793@lists.php.net to get a copy of this message | ||
Hello! I was founding a function, which finds any occurence of a string (no: first occurence). I
wasn't, so I maked this function! It may be very useful.
<?php
int strnpos(string $haystack, mixed $needle, int $occurence);
?>
Example:
<?php
strnpos("I like the bananas. You like coke. We like chocolate.", "like", 2); //
24
?>
Here's code of this function:
<?php
function strnpos($base, $str, $n)
{
if ($n <= 0 || intval($n) != $n || substr_count($base, $str) < $n) return FALSE;
$str = strval($str);
$len = 0;
for ($i=0 ; $i<$n-1 ; ++$i)
{
if ( strpos($base, $str) === FALSE ) return FALSE;
$len += strlen( substr($base, 0, strpos($base, $str) + strlen($str)) );
$base = substr($base, strpos($base, $str) + strlen($str) );
}
return strpos($base, $str) + $len;
}
?>
----
Server IP: 82.160.94.10
Probable Submitter: 83.9.163.149
----
Manual Page -- http://www.php.net/manual/en/function.strpos.php
Edit -- https://master.php.net/note/edit/83437
Del: integrated -- https://master.php.net/note/delete/83437/integrated
Del: useless -- https://master.php.net/note/delete/83437/useless
Del: bad code -- https://master.php.net/note/delete/83437/bad+code
Del: spam -- https://master.php.net/note/delete/83437/spam
Del: non-english -- https://master.php.net/note/delete/83437/non-english
Del: in docs -- https://master.php.net/note/delete/83437/in+docs
Del: other reasons-- https://master.php.net/note/delete/83437
Reject -- https://master.php.net/note/reject/83437
Search -- https://master.php.net/manage/user-notes.php