Re: Re: removing text from a string

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

 



Replying to myself now.

On Tue, Nov 4, 2008 at 7:40 AM, Yeti <yeti@xxxxxxxxxx> 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

-- 
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