note 106406 modified in function.trim by danbrown

From: Date: Tue, 15 Nov 2011 16:51:42 +0000
Subject: note 106406 modified in function.trim by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-184509@lists.php.net to get a copy of this message
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&%$ ' --was-- trim_chars (trim string or character off beginning and end of a string) 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: echo trim_chars('&%$abc&%$', '&%$'); echo trim_chars(' &%$abc&%$ ', '&%$', true); echo trim_chars(' %%%abc%%% ', '&%$', false); echo trim_chars('&%$abc&%$ ', '&%$', false); outputs: 'abc' 'abc' ' &%$abc&%$ ' 'abc&%$ ' http://php.net/manual/en/function.trim.php

« previous php.notes (#184509) next »