Remove all non-numeric characters from a string; [^0-9] doesn't match as expected

Viewed 134228

I'm trying to remove everything from a string but just numbers (0-9).

I thought this would work..

echo preg_replace("[^0-9]","",'604-619-5135');

But it echos "604-619-5135". What am I missing???

4 Answers

a much more practical way for those who do not want to use regex:

$data = filter_var($data, FILTER_SANITIZE_NUMBER_INT);

note: it works with phone numbers too.

Related