note 46342 added to function.strrpos
| From: | nexman at playoutloud dot net | Date: | Thu, 07 Oct 2004 16:22:04 +0000 |
| Subject: | note 46342 added to function.strrpos | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-78143@lists.php.net to get a copy of this message | ||
Function like the 5.0 version of strrpos for 4.x.
This will return the *last* occurence of a string within a string.
function strepos($haystack, $needle, $offset=0) {
$pos_rule = ($offset<0)?strlen($haystack)+($offset-1):$offset;
$last_pos = false; $first_run = true;
do {
$pos=strpos($haystack, $needle, (intval($last_pos)+(($first_run)?0:strlen($needle))));
if ($pos!==false && (($offset<0 && $pos <= $pos_rule)||$offset >= 0)) {
$last_pos = $pos;
} else { break; }
$first_run = false;
} while ($pos !== false);
if ($offset>0 && $last_pos<$pos_rule) { $last_pos = false; }
return $last_pos;
}
If my math is off, please feel free to correct.
- A positive offset will be the minimum character index position of the first character allowed.
- A negative offset will be subtracted from the total length and the position directly before will
be the maximum index of the first character being searched.
returns the character index ( 0+ ) of the last occurence of the needle.
* boolean FALSE will return no matches within the haystack, or outside boundries specified by the
offset.
----
Manual Page -- http://www.php.net/manual/en/function.strrpos.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+46342
Delete -- http://master.php.net/manage/user-notes.php?action=delete+46342&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+46342&report=yes
Search -- http://master.php.net/manage/user-notes.php