Re: Re: php4 vs. php5

From: Date: Fri, 03 Feb 2006 18:19:02 +0000
Subject: Re: Re: php4 vs. php5
References: 1 2 3 4 5  Groups: php.general 
Request: Send a blank email to php-general+get-229825@lists.php.net to get a copy of this message
On 2/3/06, Keith Proctor <keith_proctor@filemaker.com> wrote: I'd like to convert a string such as 'x5' to the number 5.
At 09:10 AM 2/3/2006, Rory Browne wrote:
preg_replace("/[^0-9]/", "", $x) I would expect intval to work as well, but aparently not.
Rory, the RegExp you suggest will strip decimal points and minus signs, turning -1.5 into 15. That's why I made exceptions for them with this regular expression:
        /[^\d\.\-]/ = not {digit or period or minus}
(I think I might have been excessive with the back-slashes, but they don't hurt...) Intval() will deliver the integer value up to the first non-numeric character, turning 1x to 1 but also turning 1.5 to 1. I don't know if the distinction between integer & floating point numbers is relevant to Keith's application, but I can imagine many for which it would. On top of all that, there are national standards for numeric presentation (such as the use of the comma as a decimal separator) but that's another story... Kind regards, Paul

« previous php.general (#229825) next »