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:
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:
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.
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:
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.
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?
smatts9, did you mean this? if( strlen($postcode) >= 4 ) { $postcode = substr($postcode, 0, -3); } PHP: