Extracting numeric from string [WAS: Re: php4 vs. php5]

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

 



At 04:52 PM 2/2/2006, Keith Proctor wrote:
I need to add two numbers together, unfortunately one of them is a
string that starts with a character.  I'd like to convert a string
such as 'x5' to the number 5.


Keith,

You could chop characters off the front until you've got a numeric:

        while (!is_numeric($sString) && $sString > "")
        {
                $sString = substr($sString, 1);
        }

or you could delete all non-numeric characters wherever they appear:

        $sString = preg_replace("/[^\d\.\-]/", "", $sString);

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