preg_match problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
gd77
Forum Newbie
Posts: 20
Joined: Sat Feb 26, 2011 6:11 pm

preg_match problem

Post by gd77 »

if(!preg_match('/^(([\+]+[0-9]{2,3})+([\-][0-9]{1,2})+([\-][0-9]{3}){2})$/',$txt)){......}

$txt will be matching +584-69-589-985. pattern: +xxx-xx-xxx-xxx
As regular ex.the code works fine but in php preg_match it doesn't.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: preg_match problem

Post by Christopher »

Do you really want all of those sub-patterns? How about just:

/^[\+][0-9]{2,3}[\-][0-9]{1,2}[\-][0-9]{3}[\-][0-9]{3}$/
(#10850)
User avatar
gd77
Forum Newbie
Posts: 20
Joined: Sat Feb 26, 2011 6:11 pm

Re: preg_match problem

Post by gd77 »

!preg_match('/^[\+][0-9]{2,3}[\-][0-9]{1,2}[\-][0-9]{3}[\-][0-9]{3}$/i')

Still same problem... :banghead:
User avatar
DigitalMind
Forum Contributor
Posts: 152
Joined: Mon Sep 27, 2010 2:27 am
Location: Ukraine, Kharkov

Re: preg_match problem

Post by DigitalMind »

Code: Select all

/^\+\d{3}-\d{2}-\d{3}-\d{3}$/
User avatar
gd77
Forum Newbie
Posts: 20
Joined: Sat Feb 26, 2011 6:11 pm

Re: preg_match problem

Post by gd77 »

still same :S
User avatar
DigitalMind
Forum Contributor
Posts: 152
Joined: Mon Sep 27, 2010 2:27 am
Location: Ukraine, Kharkov

Re: preg_match problem

Post by DigitalMind »

What are you trying to do?
User avatar
gd77
Forum Newbie
Posts: 20
Joined: Sat Feb 26, 2011 6:11 pm

Re: preg_match problem

Post by gd77 »

it's mentioned on top of the page :P :drunk:
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: preg_match problem

Post by Jonah Bron »

I don't know what you're doing, but this...

Code: Select all

preg_match('/^\+\d{3}-\d{2}-\d{3}-\d{3}$/', '+584-69-589-985');
...returns true. Maybe you should show us your code? Are you just trying to validate it, or actually extract those numbers?
User avatar
gd77
Forum Newbie
Posts: 20
Joined: Sat Feb 26, 2011 6:11 pm

Re: preg_match problem

Post by gd77 »

as you see above, trying to validate a phone number or string matching +xxx-xx-xxx-xxx as mentioned the main reg...exp. works my main problem it does notwork with preg_match....
User avatar
DigitalMind
Forum Contributor
Posts: 152
Joined: Mon Sep 27, 2010 2:27 am
Location: Ukraine, Kharkov

Re: preg_match problem

Post by DigitalMind »

it does
Post Reply