Web Design Forum: Hepl with regular expressions - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Hepl with regular expressions Rate Topic: -----

#1 User is offline   Milli05 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 64
  • Joined: 12-April 09
  • Reputation: 3
  • Gender:Female
  • Location:London
  • Experience:Beginner
  • Area of Expertise:Web Designer

Posted 12 March 2010 - 08:08 AM

Hi,
I am familiarizing myself with regular expressions. Can anyone recommend me good tutorial how these work, because I just can not get it!

I would like to write a function that validates phone numbers. They must be between 7 and 12 digits, there can be spaces or hyphens in between the digits, but can not have any letters and hyphen at the beginning.
This is what I have so far:

function ReformatPhoneNumber($number)
{
$clean_number=preg_replace("/\s|-/","",$number);

if(preg_match("/^[0-9]{7,12}$/",$clean_number,$match))
{
print "Valid phone number<br />";
print $match[0];
}
else
{
print "Invalid phone number";
}

return $clean_number;
}

The problem is that it cleans the number from hyphens at the beginning, so will accept a number that begins with a hyphen. I don't know how tell preg_match to look for 7-or 12 digits plus hypnes and spaces in between, in this: /^[0-9]{7,12}$/
Thanks for your help in advance!
0

#2 User is offline   Milli05 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 64
  • Joined: 12-April 09
  • Reputation: 3
  • Gender:Female
  • Location:London
  • Experience:Beginner
  • Area of Expertise:Web Designer

Posted 12 March 2010 - 08:56 AM

View PostMilli05, on 12 March 2010 - 08:08 AM, said:

Hi,
I am familiarizing myself with regular expressions. Can anyone recommend me good tutorial how these work, because I just can not get it!

I would like to write a function that validates phone numbers. They must be between 7 and 12 digits, there can be spaces or hyphens in between the digits, but can not have any letters and hyphen at the beginning.
This is what I have so far:

function ReformatPhoneNumber($number)
{
$clean_number=preg_replace("/\s|-/","",$number);

if(preg_match("/^[0-9]{7,12}$/",$clean_number,$match))
{
print "Valid phone number<br />";
print $match[0];
}
else
{
print "Invalid phone number";
}

return $clean_number;
}

The problem is that it cleans the number from hyphens at the beginning, so will accept a number that begins with a hyphen. I don't know how tell preg_match to look for 7-or 12 digits plus hypnes and spaces in between, in this: /^[0-9]{7,12}$/
Thanks for your help in advance!

OK, I think I got it:
function ReformatPhoneNumber($number)
{
if(preg_match("/^\d+\s|-*-*\d+$/",$number,$match))
{
//print $number."<br />";

$clean_number=preg_replace("/\s|-/","",$number);
//print "Uj szam: $clean_number<br/ >";

if(preg_match("/^[0-9]{7,12}$/",$clean_number,$match))
{
//print "Valid phone number<br />";
//print $match[0];
}
else
{
//print "Invalid phone number";
}
}
else
{
//print "Invalid phone number ";
}
return $clean_number;
}

It accepts spaces and hyphens and digits /^\d+\s|-*-*\d+$/. I am not sure if it is the best way though.
0

#3 User is offline   Milli05 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 64
  • Joined: 12-April 09
  • Reputation: 3
  • Gender:Female
  • Location:London
  • Experience:Beginner
  • Area of Expertise:Web Designer

Posted 12 March 2010 - 09:04 AM

View PostMilli05, on 12 March 2010 - 08:56 AM, said:

OK, I think I got it:
function ReformatPhoneNumber($number)
{
if(preg_match("/^\d+\s|-*-*\d+$/",$number,$match))
{
//print $number."<br />";

$clean_number=preg_replace("/\s|-/","",$number);
//print "Uj szam: $clean_number<br/ >";

if(preg_match("/^[0-9]{7,12}$/",$clean_number,$match))
{
//print "Valid phone number<br />";
//print $match[0];
}
else
{
//print "Invalid phone number";
}
}
else
{
//print "Invalid phone number ";
}
return $clean_number;
}

It accepts spaces and hyphens and digits /^\d+\s|-*-*\d+$/. I am not sure if it is the best way though.

no sorry, it allows hyphens at the beginning.
0

#4 User is offline   davewilly 

  • Dedicated Member
  • PipPip
  • View gallery
  • Group: Members
  • Posts: 155
  • Joined: 09-November 08
  • Reputation: 5
  • Gender:Male
  • Location:Bristol / Cheddar
  • Experience:Intermediate
  • Area of Expertise:Web Developer

Posted 12 March 2010 - 10:34 AM

View PostMilli05, on 12 March 2010 - 09:04 AM, said:

no sorry, it allows hyphens at the beginning.


Try this, it's a PCRE.
I've tested it in RegexBuddy.
Matches: 01934 7424522, 08457226748, 0845 722 6748, 0845 7226748, 0845-722-6748, 08453-722-6748
Non Matches: 0845372266748, 084534 7226674, 08453 7422 674, -0845 722 674

It's worth investing in a program like RegexBuddy if you use them often, I've found it a life saver.
For your benefit I will explain what each part of the expression does:



^[\d]{4,5}[-\s]?[\d]{3}[-\s]?[\d]{3,4}$

^         - Assert position at beginning of string
[\d]{4,5} - Match a single digit between 4 & 5 times
[-\s]?    - Match either a hyphen or a space 0 or 1 times
[\d]{3}   - Match a single digit 3 times
[-\s]?    - Match either a hyphen or a space 0 or 1 times
[\d]{3,4} - Match a single digit between 4 & 5 times
$         - Assert position at the end of the string

You will need to delimit the regex when using it in php:

/^[\d]{4,5}[-\s]?[\d]{3}[-\s]?[\d]{3,4}$/



You will find some outrageously huge PCRE patterns for matching a phone number, this one is very basic, but will get you started.

Dave

Edit: Wrap your code in [c0de] [/c0de] tags, replacing the 0 for a o!
1

#5 User is offline   Milli05 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 64
  • Joined: 12-April 09
  • Reputation: 3
  • Gender:Female
  • Location:London
  • Experience:Beginner
  • Area of Expertise:Web Designer

Posted 12 March 2010 - 10:43 AM

View Postdavewilly, on 12 March 2010 - 10:34 AM, said:

Try this, it's a PCRE.
I've tested it in RegexBuddy.
Matches: 01934 7424522, 08457226748, 0845 722 6748, 0845 7226748, 0845-722-6748, 08453-722-6748
Non Matches: 0845372266748, 084534 7226674, 08453 7422 674, -0845 722 674

It's worth investing in a program like RegexBuddy if you use them often, I've found it a life saver.
For your benefit I will explain what each part of the expression does:



^[\d]{4,5}[-\s]?[\d]{3}[-\s]?[\d]{3,4}$

^         - Assert position at beginning of string
[\d]{4,5} - Match a single digit between 4 & 5 times
[-\s]?    - Match either a hyphen or a space 0 or 1 times
[\d]{3}   - Match a single digit 3 times
[-\s]?    - Match either a hyphen or a space 0 or 1 times
[\d]{3,4} - Match a single digit between 4 & 5 times
$         - Assert position at the end of the string

You will need to delimit the regex when using it in php:

/^[\d]{4,5}[-\s]?[\d]{3}[-\s]?[\d]{3,4}$/



You will find some outrageously huge PCRE patterns for matching a phone number, this one is very basic, but will get you started.

Dave

Wow, thanks very much!!! Sometimes I felt that I was nearly there, but it did not work propery. I will check out RegexBuddy, it will definately help a lot!
0

#6 User is offline   davewilly 

  • Dedicated Member
  • PipPip
  • View gallery
  • Group: Members
  • Posts: 155
  • Joined: 09-November 08
  • Reputation: 5
  • Gender:Male
  • Location:Bristol / Cheddar
  • Experience:Intermediate
  • Area of Expertise:Web Developer

Posted 12 March 2010 - 10:51 AM

View PostMilli05, on 12 March 2010 - 10:43 AM, said:

Wow, thanks very much!!! Sometimes I felt that I was nearly there, but it did not work propery. I will check out RegexBuddy, it will definately help a lot!


You will find when you are learning regex there will be lots of occasions when you do a premature "YES!!!!".
Then it all becomes an anti climax when you realise you haven't cracked it. So, test test test.

Dave
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users