Re: ltrim behavior.

From: Date: Wed, 11 Mar 2009 19:38:23 +0000
Subject: Re: ltrim behavior.
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-289893@lists.php.net to get a copy of this message
On Wed, 2009-03-11 at 15:34 -0400, Robert Cummings wrote: > On Wed, 2009-03-11 at 15:07 -0400, Peter van der Does wrote: > > php > $a='data[options][name]'; > > php > echo ltrim($a,'data['); > > options][name] > > > > Just as I expected. > > > > Next one: > > php > $a='options[options][name]'; > > php > echo ltrim($a,'options['); > > ][name] > > > > UH, what? > > Not exactly what I expected. > > > > This works: > > php > $a='options[options][name]'; > > php > echo ltrim(ltrim($a,'options'),'['); > > options][name] > > It doesn't trim strings, it trims characters. The second argument is a > list of characters. I'm surprised this has worked for you up till now. I > think most people would use the following to do what you want: > > <?php > > echo preg_replace( '#^options\[#', '', $a ) > > ?> > > Or they would do test and strip: > > <?php > > if( substr( $a, 0, 8 ) === 'options[' ) > { > echo substr( $a, 8 ); > } > > ?> > > Or maybe: > > <?php > > if( preg_match( '#^options\[#', $a ) ) > { > echo substr( $a, 8 ); > } > > ?> Just because I'm a nice guy... :) here's the generic strip: <?php echo preg_replace( '#^[[:alpha:]]+[[:alnum:]]*\[#', '', $a ); ?> Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP

« previous php.general (#289893) next »