Forum Moderators: coopster

Message Too Old, No Replies

preg_replace to allow only numbers.

         

Artie_J

9:38 pm on Mar 3, 2006 (gmt 0)

10+ Year Member



I thought it was supposed to work like so:

$string contains letters, numbers, spaces, dashes, whatever...
$result = preg_replace("[^0-9]","", $string);
$result should only contain the numbers.
If it's not a 0-9, replace it with "".
Yes? No? Syntactical error?
Thanks!

RonPK

10:09 pm on Mar 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to use pattern delimiters:

$result = preg_replace("/[^0-9]/","", $string);

Artie_J

12:44 am on Mar 4, 2006 (gmt 0)

10+ Year Member



My forehead is getting sore. :)

Thank you Ron!