Correct Strings?

Discussion in 'PHP' started by dannbkk, Jun 16, 2007.

  1. #1
    I need correct strings can someone tell me if this is right for phone number and zip, i dont need anything fancy i just want however many numbers from 0-9 to be strored for both of them. This is what i have so far;

    $property_zip = nl2br($_POST['property_zip']);
    $property_phone = ereg_replace("[^0-9]", "", $_POST['property_phone']);

    can i do something like this?

    $property_zip = substr(0, 9),($_POST['property_zip']);
    $property_phone = substr(0, 9),($_POST['property_phone']);
     
    dannbkk, Jun 16, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    nl2br() will convert new line characters (\n) to the HTML equivalent <br />. It doesn't do anything related with numbers. Your ereg_replace() pattern removes anything that isn't a number from a string. Isn't this what you want to do?
     
    nico_swd, Jun 16, 2007 IP
  3. dannbkk

    dannbkk Well-Known Member

    Messages:
    1,403
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    120
    #3
    Hi Nico,

    Ok so lets say i do this;
    $property_zip = ereg_replace("[^0-9]", "", $_POST['property_zip']);
    $property_phone = ereg_replace("[^0-9]", "", $_POST['property_phone']);

    But than when i type numbers in property_phone it gives me output of numbers but with commas between them, LOL

    do i need to set mysql row differently I currently have something like;
    int(11)
     
    dannbkk, Jun 16, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    Commas? lol. Hmm try this:
    
    $property_phone = preg_replace('/\D/', null, $_POST['property_phone']);
    
    PHP:
    And you don't really need substr(), because MySQL will just cut the sting at offset 10 (offset starts at 0, it's still 11 characters), so it's impossible that the user enters too long numbers.

    Maybe you should change your field type though, because there are phone numbers with more than 11 digits. (At least if you accept international numbers with county prefix.)
     
    nico_swd, Jun 16, 2007 IP
  5. dannbkk

    dannbkk Well-Known Member

    Messages:
    1,403
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    120
    #5
    ok i changed it to the line of code you suggested and it outputted all the numbers except for the 0

    I typed in : 0840990712
    and it gave me output of : 840990712

    about the commas i fixed that. I had field set to this;
    <?=number_format($row['seller_phone']);?>
    but i changed to this
    <?=$row['seller_phone'];?>
     
    dannbkk, Jun 16, 2007 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    I think that's a problem with the INT field in the database, and not the code. Try changing it to VARCHAR. The code shouldn't allow any other characters, so it doesn't really matter in the end.
     
    nico_swd, Jun 16, 2007 IP
  7. dannbkk

    dannbkk Well-Known Member

    Messages:
    1,403
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    120
    #7
    Excellent work, yes you are right it needed to change to VARCHAR, it now works perfect, thanks very much!
     
    dannbkk, Jun 16, 2007 IP