Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Name: FBI Agent
hey all. im needing some help with a script that i'm pretty much done with. it's an address book that uses a database. i need the preg_match to go with the phone number. i need to take away all of the characters from the phone number except numbers before i insert them into the database so i need to use preg_match. the thing with that is that i dont know how i would go about taking out all of the letters and ('s and )'s and -'s so all that's left is the phone numbers. when i look at the numbers i will then format the phone number by using substr (which im already done with). so giving me a working piece of code to do this would be cool. thanks
FBI Agent
AIM: EliteAssassin187

You want to use preg_replace. This example will strip (actually replace with a null string - '') ALL characters from the $phone variable except the characters 0 through 9.
$phone = "(123) 456-7890";
$phone = preg_replace('/[^0-9]/','',$phone);
echo $phone;//Output: 1234567890

sweet, thanks. i was trying to figure that crap out on some website yesterday but i copuldnt get it with all of the +'s and $'s at the end and all of the weird symbols like [ and ]. but thanks, it fixes my problems.
FBI Agent
AIM: EliteAssassin187

Yeah, regular expressions are VERY powerful, but difficult to master. I don't completely understand them, but I have some experience with this already. So I was able to find the solution by looking at some examples and making some minor adjustments.
I can tell you that the ^ stands for not. So, ^0-9 means any character not between 0 to 9. To strip out all characters except letters and numbers you could use:
$var= preg_replace('/[^0-9a-zA-Z]/','',$var);
Michael J

hmmm, i looked at preg_match on php.net and it didnt explain too much on what all of those symbols did but after you telling me that i would be using preg_replace instead, i looked that up on php.net and they have a lot of information on there. so incase anyone else was wondering how they could do this just check it up on here at php.net and look for preg_replace
FBI Agent
AIM: EliteAssassin187

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |