Re: ltrim behavior.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux