Click to See Complete Forum and Search --> : Remove all non digit characters
xxarmoxx
Apr 21st, 2010, 02:07 PM
How can I take a string and remove all non digit characters from the string? Unfortunately my regex skills arent so hot :(
xxarmoxx
Apr 21st, 2010, 02:35 PM
This seems to be working well:
$str = "1234567890abc!@#$%^&*()-=_+[]{}|\\'\"?/><,.";
echo preg_replace("%[^0-9]%", "", $str);
Any objections?
penagate
Apr 21st, 2010, 09:27 PM
No objections. You can use \d as well as 0-9.
xxarmoxx
Jun 7th, 2010, 08:12 PM
how do i turn this into a preg_match?
I need to match if a string is all digits, if so true else false
kows
Jun 7th, 2010, 08:28 PM
if(preg_match('/^\d+$/', $str)){
echo "matched!\n";
}else{
echo "no match.\n";
}
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.