To remove anything that is not a number:
$output = preg_replace('/[^0-9]/', '', $input);
Explanation:
[0-9]
matches any number between 0 and 9 inclusively.^
negates a []
pattern.[^0-9]
matches anything that is not a number, and since we're using preg_replace
, they will be replaced by nothing ''
(second argument of preg_replace
).