pthurmond Posted January 8, 2007 Share Posted January 8, 2007 I am trying to take phone number input from users and remove everything but the numbers from the string, so that I can then format it how it wish. Is there a function to remove all non-numeric characters? Also can the explode function be used to separate a string into an array of its characters (for testing such as passwords or phone numbers)? If not, does a function exist that can?Thanks,Patrick Link to comment https://forums.phpfreaks.com/topic/33400-removing-every-non-numeric-character-from-a-string-and-character-arrays/ Share on other sites More sharing options...
fert Posted January 8, 2007 Share Posted January 8, 2007 [code]$num=preg_replace("[a-z]","",$num);[/code]That should remove all the letters Link to comment https://forums.phpfreaks.com/topic/33400-removing-every-non-numeric-character-from-a-string-and-character-arrays/#findComment-156174 Share on other sites More sharing options...
effigy Posted January 8, 2007 Share Posted January 8, 2007 1. [code=php:0]$phone_only_nums = preg_replace('/\D/', '', $phone_anything);[/code]2. [tt]str_split[/tt]? Link to comment https://forums.phpfreaks.com/topic/33400-removing-every-non-numeric-character-from-a-string-and-character-arrays/#findComment-156177 Share on other sites More sharing options...
pthurmond Posted January 8, 2007 Author Share Posted January 8, 2007 I am mainly concerned about special characters such as dashes and parenthesis. Link to comment https://forums.phpfreaks.com/topic/33400-removing-every-non-numeric-character-from-a-string-and-character-arrays/#findComment-156179 Share on other sites More sharing options...
Psycho Posted January 8, 2007 Share Posted January 8, 2007 [quote author=pthurmond link=topic=121575.msg500141#msg500141 date=1168298233]I am mainly concerned about special characters such as dashes and parenthesis.[/quote]Then you need to be very specific. What characters fall into the "such as" category?Typically I have done phone number formatting on the client side using javascript. That is not 100% since the user's can have JS turned off, but it is a good first line of defense. But, the methodology would be the same.The process would go something like this:1. Strip all non-numerica characters from the string2. Check the length (usually must be 7 or 10 characters, but could be a range as well - depends on the situation). If the length is not correct, then alert the user to change it.3. If the length validation passes then reformat the string how it should be(e.g. (123) 456-7890 or 123-456-7890, etc.)However, you *may* want to allow alpha characters so the phone could be something like 1-800-GETMORE. So, are you wanting to exclude certain characters or are you wanting to only limit it to certain characters. If you can provide specific criteria, it will be easier to provide the solution you are looking for. Link to comment https://forums.phpfreaks.com/topic/33400-removing-every-non-numeric-character-from-a-string-and-character-arrays/#findComment-156190 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.