note 106406 deleted from function.trim by hholzgra
| From: | hholzgra@php.net | Date: | Thu, 02 Aug 2012 12:32:29 +0000 |
| Subject: | note 106406 deleted from function.trim by hholzgra | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-189679@lists.php.net to get a copy of this message | ||
Note Submitter: Jesse Oberlin
----
trim_chars
(trim string or character off beginning and end of a string)
<?php
function trim_chars($string,$char,$whitespace_first=null){
$string = ($whitespace_first ? trim($string) : $string);
$trimmed = ( substr($string,-1)==$char ? substr($string,($string[0]==$char ? 1 : 0),-1) :
substr($string,($string[0]==$char ? 1 : 0)) );
return ( $trimmed == $string ? $trimmed : trim_str($trimmed,$char) );
}
?>
usage:
<?php
echo trim_chars('&%$abc&%$', '&%$');
echo trim_chars(' &%$abc&%$ ', '&%$', true);
echo trim_chars(' %%%abc%%% ', '&%$', false);
echo trim_chars('&%$abc&%$ ', '&%$', false);
?>
outputs:
'abc'
'abc'
' &%$abc&%$ '
'abc&%$ '