#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,650
    Rep Power
    171

    Solid regext and substr


    Hi;

    I need to make sure I end up with a number with this format:

    1 - Start with 61
    2 - Followed by 9 digits.

    For example all numbers below should end up like this:61405333535

    +61405333535
    61405333535
    0405333535
    0405 333 535
    .
    .
    .

    So far I have this and seems to work fine. Any tips or issues I missed?

    Thanks
    PHP Code:
    $phone $_GET['phone'];
    //remove everything but numbers
    $phone preg_replace("/[^0-9]/","",$phone);

    //Remove 0 from begining if exists 
    if (substr($phone01) === '0')
        {
            
    $phone ltrim ($phone,'0');    
        }

    //If already has 61, remove it
    if(substr($phone,0,2) == '61')
         {
              
    $phone ltrim ($phone,'61');    
         }
    $phone "61".$phone;    
    echo 
    $phone
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    this is actually wrong. In any case, it's very cumbersome and user-hostile.

    The function call ltrim($phone, '61') removes every "6" and "1" from the beginning of the number. So if my phone number happens to be +61666111666, I end up with an empty string. The second argument of ltrim() is treated as a list of characters, not a single prefix. And the function removes as many characters as possible. See the manual.

    Silently modifying user data is generally a very bad idea. I do not want you to somehow guess my phone number. If I make a mistake, I want to be told so that I can correct it.

    Start by defining the acceptable input. It would be something like this (after you've removed the whitespace):

    Code:
    /\A(?:0|\+61)(\d{9})\z/
    You can make it more permissive, but all obvious mistakes should be rejected.

    The end-user number is stored in the first capturing group. So you can take it and simply prepend "61" (shouldn't that be "+61"?). This automatically gives you the desired format.
    Last edited by Jacques1; January 30th, 2014 at 12:34 AM.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,650
    Rep Power
    171
    Originally Posted by Jacques1
    Hi,

    this is actually wrong. In any case, it's very cumbersome and user-hostile.

    The function call ltrim($phone, '61') removes every "6" and "1" from the beginning of the number. So if my phone number happens to be +61666111666, I end up with an empty string. The second argument of ltrim() is treated as a list of characters, not a single prefix. And the function removes as many characters as possible. See the manual.

    Silently modifying user data is generally a very bad idea. I do not want you to somehow guess my phone number. If I make a mistake, I want to be told so that I can correct it.

    Start by defining the acceptable input. It would be something like this (after you've removed the whitespace):

    Code:
    /\A(?:0|\+61)(\d{9})\z/
    You can make it more permissive, but all obvious mistakes should be rejected.

    The end-user number is stored in the first capturing group. So you can take it and simply prepend "61" (shouldn't that be "+61"?). This automatically gives you the desired format.
    That is how they have desgined it. I thought +61 would make more sense but they want to see the number start with 61.

IMN logo majestic logo threadwatch logo seochat tools logo