On 2/3/06, Keith Proctor <keith_proctor@xxxxxxxxxxxxx> 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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php