Re: ltrim behavior.
| From: | Robert Cummings | Date: | Wed, 11 Mar 2009 19:34:54 +0000 |
| Subject: | Re: ltrim behavior. | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-289892@lists.php.net to get a copy of this message | ||
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 );
}
?>
Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP