www.webdeveloper.com
Results 1 to 7 of 7

Thread: REgualr expressions help

  1. #1
    Join Date
    Jul 2013
    Posts
    18

    REgualr expressions help

    Hi all,

    I want to create a php regex that will accept Egyptian phone numbers (a mobile number is 11 digits) ..

    Egyptian phone numbers look like

    0119799612
    But it may be written in the following format :
    011 97 99 612
    011-97-99-612

    Also a prefix of one of the following may be present and mayn't

    02
    202
    +2

    Can some help ?

  2. #2
    Join Date
    Feb 2011
    Posts
    231
    Hi,
    The numbers from your examples have 10 digits (not 11).
    Try rthe regexp from this example:
    Code:
    $nr = '011-97-99-612';
    $rgx = '/^(02|202|\+2){0,1}[0-9]{3}[ -]{0,1}[0-9]{2}[ -]{0,1}[0-9]{2}[ -]{0,1}[0-9]{3}$/';
    if(preg_match($rgx, $nr, $m)) var_export($m);

  3. #3
    Join Date
    Jul 2013
    Posts
    18
    The numbers from your examples have 10 digits (not 11).
    Sorry , It was wrong ..

    The right example is 11 digits : 01119799612

    The regex you wrote will only match if the spaces and dashes are in the exact position as the example .. But this is not sure .. Spaces may be anywhere , It may look like : 0111 979 9618 , and may be 01 119 799 618

    I mean, They are exactly 11 numbers (with or without prefix ) , With spaces and dashes may be put anywhere. .

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    I would just strip out anything that is not a number. Then you can use strlen() to figure out how many digits there are.
    PHP Code:
    $number preg_replace('/\D/'''$number);
    if(
    strlen($number) != 11) {
      
    // invalid number

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Jul 2013
    Posts
    18
    Thank you NogDog ,
    But your code will not be so accurate ..

    Wt if there is a prefix ?
    Wt if there are spaces or dashes ??

  6. #6
    Join Date
    Jul 2013
    Posts
    18
    Any help ?

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    Quote Originally Posted by gemswebs View Post
    Thank you NogDog ,
    But your code will not be so accurate ..

    Wt if there is a prefix ?
    Then you need to explicitly define what the allowable string lengths are, and test in an "or" condition, add code to strip out the prefix is you don't actually need/want it, etc.
    Wt if there are spaces or dashes ??
    As I stated above, you first use preg_replace() to strip out any character that is not a number.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles