homepage Welcome to WebmasterWorld Guest from 54.226.136.179
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Need help on this REGEXP
dbarasuk

5+ Year Member



 
Msg#: 4376569 posted 2:42 pm on Oct 19, 2011 (gmt 0)

Hi all,
I have a small code snippet that validates fixed national telephone numbers. Our telephone code always begins by 22 followed eventually by a space. The next 2 figures are between the range of 21 and 30 but exclude figures 28 and 29. At the end of a valid number are always 4 figures between 0 and 9. There can be spaces between two consecutive figures.

I have the next code snippet for validation:

<?php
if(isset($_POST['tel'])){
$telephone = htmlentities($_POST['telephone']);

if(preg_match("#^22[ ]?([21-27]|30)([ ]?[0-9]{2}){2}$#", $telephone)){
echo $telephone ." is a valid <strong>number</strong>";

}else{
echo $telephone ." is not a valid number, please correct it";
}
}
?>

If i enter 22 30 22 59 (with or without space) the number is found to be valid, however if i enter 22 21 10 57 with or without spaces the number is rejected. What could be wrong, please?

Your help is more than appreciated

 

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4376569 posted 6:39 pm on Oct 19, 2011 (gmt 0)

I assume "eventually" means (in English) "optionally". (I know languages where this form occurs, though I gotta say they don't match your name.) Also that "figures" means "group of digits", not "(individual) digits".

[21-27] does not mean
"any two-digit number in the set 21 22 23 24 25 26 27"
It means
"any one-digit number in the set 2 1-2 7-- i.e. 1 2 7"

Similarly
([ ]?[0-9]{2}){2}
does not mean "four numerals, optionally preceded by a space".
It means "two numerals, optionally preceded by a space, and then the identical group again".

It is safer to get rid of the spaces beforehand. Either design your input to ignore them (one way is to have three separate boxes, each of which skips to the next box as soon as the user has entered the right number of digits), or strip them out before you run any code.

I make it

22(2[1-7]|30)[0-9]{4}
or
22(2[1-7]|30)\d{4} (if your setup supports the \d notation)

although \d\d\d\d may be just as fast. If you need to capture, put the whole thing in parentheses as (for example)

(22(2[1-7]|30)[0-9]{4})

[edited by: lucy24 at 7:05 pm (utc) on Oct 19, 2011]

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4376569 posted 6:57 pm on Oct 19, 2011 (gmt 0)

Which African country is this for?

Readie

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4376569 posted 7:24 pm on Oct 20, 2011 (gmt 0)

Although I agree with Lucy - if you don't want to strip spaces:
/^22\ ?(2[1-7]|30)(\ ?\d{2}){2}$/

However:

([ ]?[0-9]{2}){2}
...
It means "two numerals, optionally preceded by a space, and then the identical group again".

I have never found this to be the case when working with PCRE in PHP. I find that it allows the second match to differ from the first.

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4376569 posted 8:38 am on Oct 21, 2011 (gmt 0)

I have never found this to be the case when working with PCRE in PHP. I find that it allows the second match to differ from the first.

I think you're right, and not just for php. I now remember doing analogous things in text editing. The two pieces only match if you say explicitly ([ ]?[0-9]{2})\1 (or whatever your dialect requires for the inline capture).

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved