How do I ditch the last 2 characters in a value?
I have built a MYSQL database driven website based on the common postcode for an area. By that I mean the county code (say GL) the county split (say 2) and the district (say 4) - this example would have a common postcode of GL2 4 (an actual postcode would be GL2 4AA for example).
The idea is that a user enters a postcode on a form, PHP converts the data to a variable, the common postcode for that area is identified from the variable is sent to the MYSQL database and the information is returned to user.
In an ideal world I would simply use a select statement that only reads the first 4 digits of the variable (such as LEFT($value,4)) but this falls down as unfortunately some of the county splits are 2 digits long (thus postcodes such as GL50 4AA).
What I need is a way to disregard the last two digits (letters) of the postcode to extract the common postcode. This is the only foolproof filter I can identify.
Please let me know if you have any ideas how to do this - I have tried RIGHT($value,-2) in my select statement but it did not work!
Thanks.
Ditching characters in a value
Moderator: General Moderators
Aha. Thanks.feyd wrote:[php_man]substr[/php_man]
So on that basis, would this work?
Code: Select all
<?php
if
strlen($value)>6
$searchvalue = substr("$value", 0, 5);
else
$searchvalue = substr("$value", 0, 4);
?>- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Code: Select all
substr($value,0,strlen($value) - 2);Magic! Will try it tonight & let you know.feyd wrote:Code: Select all
substr($value,0,strlen($value) - 2);
Thanks Feyd!!!