John
John

Reputation: 11

Why is preg_match() not returning for number format?

I know this is a simple question. I can't seem to find the answer. I am trying to get the number only.. but it returns 1.. I am not sure what I am doing wrong. Any help

$test = preg_match("/[0-9]/","(469) 552-6500");

Upvotes: 0

Views: 30

Answers (2)

Zak
Zak

Reputation: 7515

If you are trying to return only the numbers -- You should be using preg_replace in my opinion. Also you need the / and delimiters.

echo preg_replace('/[^0-9]/', '', '(469) 552-6500');  

This will return:

4695526500

Upvotes: 3

Jay Blanchard
Jay Blanchard

Reputation: 34416

Echoing $test will return 1 (true) if there is a match and 0 (false) if there isn't a match but you must use delimiters ( / ) around your "needle". Better to store the matched array and test against the returned array:

$test = preg_match("/[0-9]/","(469) 552-6500", $matches);
print_r($matches);

Returns:

Array
(
    [0] => 4
)

Upvotes: 1

Related Questions