Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Regular Coder Jesper Møller's Avatar
    Join Date
    Jun 2006
    Location
    Denmark, Copenhagen
    Posts
    135
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Smile Finding plus(+) sign in string

    Hi
    Im rather new to PHP (only been at it a week) so i hope someone can help me

    I want to check a string to see if its a valid phonenumber, so it may contain only numbers and spaces AND it may also contain a single plus(+) sign for contry code, but only at the beginning of the number

    this is what i have come up with so far:

    First chekking the string only have numbers, spaces and +sign
    PHP Code:
    if (preg_match("/^([0-9 \+]+)$/"$string)) { 
    Then chekking that ther is only +sign
    PHP Code:
    if (preg_match_all("/[\+]/"$string)==1) {
        
    $check2 'Landcode ok<br>';
        }
    else
        {
        
    $check2 'Landcode  NOT ok<br>';
        } 
    and its this check i cant get to work ??? no mater
    Last edited by Jesper Møller; 02-15-2010 at 03:55 AM.
    "True knowledge exists in knowing that you know nothing."

    "Education is learning what you didn't even know you didn't know!"

  • #2
    New Coder
    Join Date
    Dec 2008
    Posts
    16
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Please try the following


    PHP Code:

    if (preg_match("/^(\+)*([0-9]+)$/"$string)) { 

      echo 
    "Okay";

    } else {

      echo 
    "not a land number";

    Dedicated Servers - sales@hivelocity.net - 1-888-869-HOST(4678)
    Award winning Managed Hosting - Dedicated Server Hosting
    Managed Dedicated Servers. Reseller Discounts. 24/7 Impressive Tech Support.

  • #3
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Code:
    if (preg_match("/^\+?([\d\s]+)$/", $string)) {
    Last edited by MattF; 02-13-2010 at 06:47 AM.

  • #4
    Regular Coder Jesper Møller's Avatar
    Join Date
    Jun 2006
    Location
    Denmark, Copenhagen
    Posts
    135
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Thanks

    As i see it i wasn't totaly wrong
    But that looks mutch easyer

    Only thing missing was the space but got that with [0-9 ]

    now i haw to find out hov/why that works ?

    i gues (if i have read my tutorials corect) that the ^(\+) meens look for a + ONLY in the beginning
    the * is like a + but may only ocur once
    [0-9 ]+ is looking for number and spaces and the space meens the may ocur more than once
    and the $ meen its shal keep on looking until the end

    ??
    "True knowledge exists in knowing that you know nothing."

    "Education is learning what you didn't even know you didn't know!"

  • #5
    Regular Coder Jesper Møller's Avatar
    Join Date
    Jun 2006
    Location
    Denmark, Copenhagen
    Posts
    135
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Thanks ... thant workd to

    almost the same .. but with \d for digits and \s for spaces right ??

    but that ? and no () around \+ ???
    "True knowledge exists in knowing that you know nothing."

    "Education is learning what you didn't even know you didn't know!"

  • #6
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by Jesper Møller View Post
    Thanks ... thant workd to

    almost the same .. but with \d for digits and \s for spaces right ??

    but that ? and no () around \+ ???
    You aren't capturing the plus sign? No need for the capturing parantheses, if so. The ? in the expression states that the preceding character may or may not exist, but if it does it will match one, and only one, plus sign, whereas the * in the first example posted will match none of or any number of + at the beginning of the string. Try it with several ++ at the beginning of the string and you'll see what I mean. The \d and \s, as you correctly surmise, are merely shorthand.


    Edit: In fact, you aren't capturing anything with that expression, so you can remove all parantheses:

    Code:
    if (preg_match("/^\+?[\d\s]+$/", $string)) {
    If you want to use it as a check and capture expression, use:

    Code:
    if (preg_match("/^(\+?[\d\s]+)$/", $string, $matches)) {
    Last edited by MattF; 02-13-2010 at 07:08 AM.

  • Users who have thanked MattF for this post:

    Jesper Møller (02-13-2010)

  • #7
    Regular Coder Jesper Møller's Avatar
    Join Date
    Jun 2006
    Location
    Denmark, Copenhagen
    Posts
    135
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Your right .. didnt test with 2+ signs ind the begining

    BTW.
    Is it posible to "revers" that line so the if state only is tru if it dont match

    Thinking loosing the else state
    somthing like:
    ifnot (preg_match("/^\+*([\d\s]+)$/", $string)) {
    echo ('WRONG');
    }
    "True knowledge exists in knowing that you know nothing."

    "Education is learning what you didn't even know you didn't know!"

  • #8
    Regular Coder Jesper Møller's Avatar
    Join Date
    Jun 2006
    Location
    Denmark, Copenhagen
    Posts
    135
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by MattF View Post
    You aren't capturing the plus sign? No need for the capturing parantheses, ......

    Edit: In fact, you aren't capturing anything with that expression, so you can remove all parantheses:
    Im not shure i know what is ment by capturing
    dictionary says To take captive,seize, To gain possession or contro ??

    Sorry my english aint that good (Im a little dyslexie)
    "True knowledge exists in knowing that you know nothing."

    "Education is learning what you didn't even know you didn't know!"

  • #9
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    For a direct non match, use:

    Code:
    if (!preg_match("/^\+?[\d\s]+$/", $string)) {
    Last edited by MattF; 02-13-2010 at 07:25 AM.

  • #10
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by Jesper Møller View Post
    Im not shure i know what is ment by capturing
    If you want to capture, (have the telephone number available for later use in your script), the capturing expression I posted would make the phone number available via the $matches[1] var.

  • #11
    Regular Coder Jesper Møller's Avatar
    Join Date
    Jun 2006
    Location
    Denmark, Copenhagen
    Posts
    135
    Thanks
    9
    Thanked 0 Times in 0 Posts
    funny tryd that befor asking... but cud not get it to work .. but now it dos ..

    May be becaus it 8am and i have been working on this all night :-P
    "True knowledge exists in knowing that you know nothing."

    "Education is learning what you didn't even know you didn't know!"

  • #12
    Regular Coder Jesper Møller's Avatar
    Join Date
    Jun 2006
    Location
    Denmark, Copenhagen
    Posts
    135
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Ahhh ... ok

    I am going to use it later ... alogn with other entrys like name and adress
    Ill look into that $matches[]
    "True knowledge exists in knowing that you know nothing."

    "Education is learning what you didn't even know you didn't know!"

  • #13
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by Jesper Møller View Post
    Ahhh ... ok

    I am going to use it later ... alogn with other entrys like name and adress
    Ill look into that $matches[]
    It's a case of either/either, to be honest. The $string var that you're checking against would, (provided it passes the preg_match check), already contain a valid phone number. The $matches var from the preg_match would only come into it's own if you were literally extracting the phone number from a longer string. The fact that you're tying the expression to the start and end of the string with ^ and $ respectively means that you're safe just using $string as your number, once it has passed the validation check.
    Last edited by MattF; 02-13-2010 at 07:46 AM.

  • #14
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    This is why I love regex buddy
    It can explain the whole regex for you. This is the explanation for MattF's regex
    Regex info

    ^\+?([\d\s]+)$

    Assert position at the beginning of the string ^
    Match the character “+” literally \+?
    Between zero and one times, as many times as possible, giving back as needed (greedy) ?
    Match the regular expression below and capture its match into backreference number 1 ([\d\s]+)
    Match a single character present in the list below [\d\s]+
    Between one and unlimited times, as many times as possible, giving back as needed (greedy) +
    A single digit 0..9 \d
    A whitespace character (spaces, tabs, line breaks, etc.) \s
    Assert position at the end of the string (or before the line break at the end of the string, if any) $
    Hope that helps explain the whole process for you
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • Users who have thanked JAY6390 for this post:

    Jesper Møller (02-13-2010)

  • #15
    Regular Coder Jesper Møller's Avatar
    Join Date
    Jun 2006
    Location
    Denmark, Copenhagen
    Posts
    135
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Thanks for all your help.

    Learning php is mutch easyer when peapol are so helpsful explaining :-)
    "True knowledge exists in knowing that you know nothing."

    "Education is learning what you didn't even know you didn't know!"


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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