note 38330 added to function.strrpos
| From: | cagret at yahoo dot com | Date: | Wed, 17 Dec 2003 02:12:28 +0000 |
| Subject: | note 38330 added to function.strrpos | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-62100@lists.php.net to get a copy of this message | ||
This functions could be helpful:
<?php
/**
* @access public
* @package String
* @static
*/
class String {
/**
* Replace only the first occurence of the search string with the replacement string
* @param string $string
* @param string $search
* @param string $replace
* @return string
* @access public
*/
function replaceFirst($string, $search, $replace) {
$pos = strpos($string, $search);
if (is_int($pos)) {
$len = strlen($search);
return substr_replace($string, $replace, $pos, $len);
}
return $string;
}
/**
* Replace only the last occurence of the search string with the replacement string
* @param string $string
* @param string $search
* @param string $replace
* @return string
* @access public
*/
function replaceLast($string, $search, $replace) {
$pos = false;
if (is_int(strpos($string, $search))) {
$endPos = strlen($string);
while ($endPos > 0) {
$endPos = $endPos - 1;
$pos = strpos($string, $search, $endPos);
if (is_int($pos)) {
break;
}
}
}
if (is_int($pos)) {
$len = strlen($search);
return substr_replace($string, $replace, $pos, $len);
}
return $string;
}
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.strrpos.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+38330
Delete -- http://master.php.net/manage/user-notes.php?action=delete+38330&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+38330&report=yes
Search -- http://master.php.net/manage/user-notes.php