note 82307 modified in function.rtrim by danbrown
| From: | danbrown@php.net | Date: | Sun, 14 Dec 2008 17:21:40 +0000 |
| Subject: | note 82307 modified in function.rtrim by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-148151@lists.php.net to get a copy of this message | ||
I'm sure there's a better way to strip strings from the end of strings.
<?php
/**
* Strip a string from the end of a string
*
* @param string $str the input string
* @param string $remove OPTIONAL string to remove
*
* @return string the modified string
*/
function rstrtrim($str, $remove=null)
{
$str = (string)$str;
$remove = (string)$remove;
if(empty($remove))
{
return rtrim($str);
}
$len = strlen($remove);
$offset = strlen($str)-$len;
while($offset > 0 && $offset == strpos($str, $remove, $offset))
{
$str = substr($str, 0, $offset);
$offset = strlen($str)-$len;
}
return rtrim($str);
} //End of function rstrtrim($str, $remove=null)
echo rstrtrim('Hello World!!!', '!') .'<br />'; //"Hello
World"
echo rstrtrim('Hello World!!!', '!!') .'<br />'; //"Hello
World!"
echo rstrtrim('Hello World!!!', '!!!') .'<br />'; //"Hello
World"
echo rstrtrim('Hello World!!!', '!!!!').'<br />'; //"Hello
World!!!"
?>
--was--
I'm sure there's a better way to strip strings from the end of strings.
/**
* Strip a string from the end of a string
*
* @param string $str the input string
* @param string $remove OPTIONAL string to remove
*
* @return string the modified string
*/
function rstrtrim($str, $remove=null)
{
$str = (string)$str;
$remove = (string)$remove;
if(empty($remove))
{
return rtrim($str);
}
$len = strlen($remove);
$offset = strlen($str)-$len;
while($offset > 0 && $offset == strpos($str, $remove, $offset))
{
$str = substr($str, 0, $offset);
$offset = strlen($str)-$len;
}
return rtrim($str);
} //End of function rstrtrim($str, $remove=null)
echo rstrtrim('Hello World!!!', '!') .'<br />'; //"Hello
World"
echo rstrtrim('Hello World!!!', '!!') .'<br />'; //"Hello
World!"
echo rstrtrim('Hello World!!!', '!!!') .'<br />'; //"Hello
World"
echo rstrtrim('Hello World!!!', '!!!!').'<br />'; //"Hello
World!!!"
http://php.net/manual/en/function.rtrim.php