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!
Page 1 of 1
Hepl with regular expressions
#2
Posted 12 March 2010 - 08:56 AM
Milli05, 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!
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.
#3
Posted 12 March 2010 - 09:04 AM
Milli05, 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.
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.
#4
Posted 12 March 2010 - 10:34 AM
Milli05, 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!
#5
Posted 12 March 2010 - 10:43 AM
davewilly, 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:
You will find some outrageously huge PCRE patterns for matching a phone number, this one is very basic, but will get you started.
Dave
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!
#6
Posted 12 March 2010 - 10:51 AM
Milli05, 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
- ← PHP Drop down list, how can i add images?
- Server Side (PHP, Databases, ASP.NET, etc)
- Newbie learning php & sql needing help with a small mattter →
Share this topic:
Page 1 of 1
Help

















