[php] PHP preg replace only allow numbers

How can I modify this existing preg_replace to only allow numbers?

function __cleanData($c) 
{
    return preg_replace("/[^A-Za-z0-9]/", "",$c);
}

This question is related to php preg-replace

The answer is


This should do what you want:

preg_replace("/[^0-9]/", "",$c);

You could also use T-Regx library:

pattern('\D')->remove($c)

T-Regx also:

  • Throws exceptions on fail (not false, null or warnings)
  • Has automatic delimiters (delimiters are not required!)
  • Has a lot cleaner api

Try this:

return preg_replace("/[^0-9]/", "",$c);