How to remove all non-numeric characters from a string in PHP?

I asked a question about how to remove all characters except numbers in javascript here.

I want to do the same with PHP. Can anyone help?

You can use the same regex expression here in PHP. Just use preg_replace() function instead of JavaScript’s replace() function.

echo preg_replace('/\D/', "", $string);

Or

echo preg_replace('/[^0-9]/', "", $string);