PHP preg replace only allow numbers

PhpPreg Replace

Php Problem Overview


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

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

Php Solutions


Solution 1 - Php

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)

Solution 2 - Php

Try this:

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

Solution 3 - Php

This should do what you want:

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

Solution 4 - Php

You can also use prune() if you have tregx:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRyanView Question on Stackoverflow
Solution 1 - PhplonesomedayView Answer on Stackoverflow
Solution 2 - Phpqbert220View Answer on Stackoverflow
Solution 3 - PhpoeziView Answer on Stackoverflow
Solution 4 - PhpDanonView Answer on Stackoverflow