SyntaxFix
Write A Post
Hire A Developer
Questions
Use \D to match non-digit characters.
\D
preg_replace('~\D~', '', $str);