1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Strip last three digits from inputted form field?

Discussion in 'Databases' started by jamus, Aug 10, 2006.

  1. #1
    Is it possible to strip off the last three digits from inputted form field?

    at the moment Im using the first four digits, but this is no good for postcodes.

    eg. "TQ1 3BJ" and "TQ13" would be confused (like me:))

    How do i work from the other end? As postcodes only ever have three digits on the end.

    something like

    // form is submitted
    if ($_SERVER['REQUEST_METHOD'] == 'POST')
    {
    	$area = $_POST['searchVal'];
    	$postcode = trim(strtoupper($_POST['pcode1'])); // collect postcode from form - trip whitespace & cut spaces and convert to uppercase
    	$postcode = substr($postcode -loose last 3) <<<  strip off last three digits?????
          $distance = getDistance($postcode,$area); // Do the search
    
    }
    PHP:

     
    jamus, Aug 10, 2006 IP
  2. tflight

    tflight Peon

    Messages:
    617
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If I understand what you are looking for, here is what you will need. If $postcode started as "TQ1 3BJ" you will end up with "TQ1 " in this example.

    $postcode = substr($postcode, 0, -3) // strip off last three digits
    PHP:
     
    tflight, Aug 10, 2006 IP
  3. jamus

    jamus Peon

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you thank you thank you. nice one.
    im begining to like this place!
     
    jamus, Aug 10, 2006 IP
  4. jamus

    jamus Peon

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    ahh. but what happens if they only start with 3 digits. Then you end up with none!

    I need to think about validating the code. or maybe limiting the numbers entered?!?? hmm.

    thanks for your help though.
     
    jamus, Aug 11, 2006 IP
  5. tflight

    tflight Peon

    Messages:
    617
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Then perhaps you want to just look at the first three or first four characters and drop anything else?
    $postcode = substr($postcode, 0, 4) // only look at the first four characters
    PHP:
     
    tflight, Aug 11, 2006 IP
  6. jamus

    jamus Peon

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Nice idea,

    but wouldn't that take me back to my original problem?

    I think im going to state "ONLY first part of postcode" then restrict the form input to 4 characters.
     
    jamus, Aug 11, 2006 IP
  7. smatts9

    smatts9 Active Member

    Messages:
    1,089
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    88
    #7
    Use an if statement?

    if($postcode >= 4) {
    $postcode = substr($postcode, 0, -3);
    }
     
    smatts9, Aug 11, 2006 IP
  8. jamus

    jamus Peon

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Great idea.

    Do you think it would look better to ask for the whole postcode and use above method smatts9 suggested, only actually using the first part of the postcode.

    or restrict the form so people can only enter the first part?
     
    jamus, Aug 11, 2006 IP
  9. tflight

    tflight Peon

    Messages:
    617
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    0
    #9
    smatts9, did you mean this?

    if( strlen($postcode) >= 4 ) {
    $postcode = substr($postcode, 0, -3);
    }
    PHP:
     
    tflight, Aug 11, 2006 IP
  10. smatts9

    smatts9 Active Member

    Messages:
    1,089
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    88
    #10
    Yes, it is kind of early, thank you.
     
    smatts9, Aug 11, 2006 IP