Extracting numeric from string [WAS: Re: php4 vs. php5]
| From: | Paul Novitski | Date: | Fri, 03 Feb 2006 01:09:31 +0000 |
| Subject: | Extracting numeric from string [WAS: Re: php4 vs. php5] | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-229786@lists.php.net to get a copy of this message | ||
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