note 72466 added to function.strpos
| From: | jamie at jamiechong dot ca | Date: | Fri, 19 Jan 2007 20:15:47 +0000 |
| Subject: | note 72466 added to function.strpos | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-121928@lists.php.net to get a copy of this message | ||
Try this function to find the first position of needle before a given offset.
For example:
<?php
$s = "This is a test a is This";
$offset = strpos($s, "test");
strnpos($s, "is", $offset); // returns 17
strnpos($s, "is", -$offset); // returns 5
// Works just like strpos if $offset is positive.
// If $offset is negative, return the first position of needle
// before before $offset.
function strnpos($haystack, $needle, $offset=0)
{
if ($offset>=0)
$result=strpos($haystack, $needle, $offset);
else
{
$offset=strlen($haystack)+$offset;
$haystack=strrev($haystack);
$needle=strrev($needle);
$result=strpos($haystack, $needle, $offset);
if ($result!==false)
{
$result+=strlen($needle);
$result=strlen($haystack)-$result;
}
}
return $result;
}
?>
----
Server IP: 66.207.199.35
Probable Submitter: 24.86.134.53
----
Manual Page -- http://www.php.net/manual/en/function.strpos.php
Edit -- https://master.php.net/note/edit/72466
Del: integrated -- https://master.php.net/note/delete/72466/integrated
Del: useless -- https://master.php.net/note/delete/72466/useless
Del: bad code -- https://master.php.net/note/delete/72466/bad+code
Del: spam -- https://master.php.net/note/delete/72466/spam
Del: non-english -- https://master.php.net/note/delete/72466/non-english
Del: in docs -- https://master.php.net/note/delete/72466/in+docs
Del: other reasons-- https://master.php.net/note/delete/72466
Reject -- https://master.php.net/note/reject/72466
Search -- https://master.php.net/manage/user-notes.php