Computing.Net > Forums > Web Development > PHP preg_match

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

PHP preg_match

Reply to Message Icon

Name: FBI Agent
Date: August 31, 2005 at 14:56:42 Pacific
OS: Windows XP Home
CPU/Ram: 1.79GHz Celeron/ 384MB DD
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Michael J (by mjdamato)
Date: August 31, 2005 at 22:44:20 Pacific
Reply:

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


0

Response Number 2
Name: FBI Agent
Date: September 1, 2005 at 10:29:04 Pacific
Reply:

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


0

Response Number 3
Name: Michael J (by mjdamato)
Date: September 1, 2005 at 12:08:22 Pacific
Reply:

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


0

Response Number 4
Name: FBI Agent
Date: September 1, 2005 at 15:31:29 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Web Development Forum Home


Sponsored links

Ads by Google


Results for: PHP preg_match

String replacement problem in PHP www.computing.net/answers/webdevel/string-replacement-problem-in-php/675.html

php.. ? www.computing.net/answers/webdevel/php-/974.html

PHP code query www.computing.net/answers/webdevel/php-code-query/1124.html