0

I have tried everything but answer is probably much simple. Variable $data['phone'] is for example 954a23589 and I want only numbers so ill get them.

$phoneW = strval($data['phone']);
preg_match_all('!\d+!', $phoneW, $matches);
print_r(array_values($matches));  echo '<br /><br />';

The output is

Array ( [0] => Array ( [0] => 954 [1] => 23589 ) ) 

I want everyone of them together as string or int (does not matter).

1
  • 1
    print_r(implode('', array_values($matches))); Commented Oct 2, 2013 at 9:04

2 Answers 2

7

It's probably easier to replace every non-digit with an empty string:

$number = preg_replace('/\D+/', '', $phoneW);
Sign up to request clarification or add additional context in comments.

3 Comments

..and then use intval($string) or (int)$string to cast it to an integer if needed.
@h2ooooooo: you cannot cast phone numbers to int, since phone numbers are normally long numbers and will probably be larger than PHP_INT_MAX.
@h2ooooooo: and sometimes contain leading zeroes that must not be removed.
0

and I want only numbers so ill get them

Why not replace everything that is not a digit with “nothing”?

echo preg_replace('#[^\d]#', '', '954a23589');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.