PHP preg replace only allow numbers

Ryan picture Ryan · Oct 7, 2011 · Viewed 122.5k times · Source

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

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

Answer

lonesomeday picture lonesomeday · Oct 7, 2011

I think you're saying you want to remove all non-numeric characters. If so, \D means "anything that isn't a digit":

preg_replace('/\D/', '', $c)