papaface Posted December 23, 2006 Share Posted December 23, 2006 Hello,Can someone please help me with the code below.[code]ereg_replace("[[:alpha:]]", "", $string);[/code]I want to allow ONLY numbers. This means i have to get rid of all special characters aswell such as <%*(£^", so i that i am just left with numbers e.g. 76445 :DThanks Quote Link to comment https://forums.phpfreaks.com/topic/31683-display-only-the-numbers-in-a-string/ Share on other sites More sharing options...
mansuang Posted December 23, 2006 Share Posted December 23, 2006 [code]ereg_replace("[^0-9]", "", $string);[/code]More information regarding regular expression : http://en.wikipedia.org/wiki/Regular_expressionhope it helps Quote Link to comment https://forums.phpfreaks.com/topic/31683-display-only-the-numbers-in-a-string/#findComment-146863 Share on other sites More sharing options...
papaface Posted December 23, 2006 Author Share Posted December 23, 2006 of course, how did i not see that.Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/31683-display-only-the-numbers-in-a-string/#findComment-146868 Share on other sites More sharing options...
papaface Posted December 23, 2006 Author Share Posted December 23, 2006 actually how would i allow only numbers AND "," commas?Ive tried[code]ereg_replace("[^0-9][^,]", "", $string);[/code]but that doesnt work. Quote Link to comment https://forums.phpfreaks.com/topic/31683-display-only-the-numbers-in-a-string/#findComment-146869 Share on other sites More sharing options...
wildteen88 Posted December 23, 2006 Share Posted December 23, 2006 add a comma after 0-9 in the character class, like so:[code=php:0]$str = 'hello123,world256';echo preg_replace("/[^0-9,]/", '', $str);// returns 123,456[/code] Quote Link to comment https://forums.phpfreaks.com/topic/31683-display-only-the-numbers-in-a-string/#findComment-146907 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.