Re: Re: removing text from a string

From: Date: Tue, 04 Nov 2008 20:28:00 +0000
Subject: Re: Re: removing text from a string
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-282877@lists.php.net to get a copy of this message
Replying to myself now. On Tue, Nov 4, 2008 at 7:40 AM, Yeti <yeti@myhich.com> wrote: >> ltrim($line, '0123456789 .'); > > I am feeling a bit boneheaded now. How easy things can be. > This would not work if the character string after the number started with a number too. EXAMPLE <?php $line = '017. 85 apples were sold to customer John Doe.'; # now ltrim would clearly cut off the '85 ' which belongs to the sentence. var_dump(ltrim($line, '0123456789 .')); ?> So if one still wanted the speedy string functions a simple trim() [1] or another ltrim() [2] would have to be added. EXAMPLE <?php $line = '017. 85 apples were sold to customer John Doe.'; var_dump(ltrim((ltrim($line, '0123456789.'), ' ')); ?> Still there is one flaw in this construct: If there was no white space between the '017.' and the '85 apples ..' we get the old mishap again. EXAMPLE <?php $line = '017.85 apples were sold to customer John Doe.'; # note the difference above: '017.85 apples ...' this time var_dump(ltrim((ltrim($line, '0123456789.'), ' ')); ?> To stick with the string functions one could either use the strpos($line, '.') + substr() [3] method or do it with explode(), implode() [4]: EXAMPLE <? $line = '017.85 apples were sold to customer John Doe.'; $line = explode('.', $line); unset($line[0]); $line = implode('.', $line); ?> So it seems that the regular expressions posted are not a bad choice after all. //A yeti [1] http://in.php.net/manual/en/function.trim.php [2] http://in.php.net/manual/en/function.ltrim.php [3] http://marc.info/?l=php-general&m=122580865009265&w=2 [4] http://in.php.net/manual/en/function.explode.php

« previous php.general (#282877) next »