Page 1 of 1

Ditching characters in a value

Posted: Mon Sep 06, 2004 7:07 am
by gojira999
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.

Posted: Mon Sep 06, 2004 7:11 am
by feyd
[php_man]substr[/php_man]

Posted: Mon Sep 06, 2004 7:29 am
by gojira999
feyd wrote:[php_man]substr[/php_man]
Aha. Thanks.

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);
?>

Posted: Mon Sep 06, 2004 7:42 am
by feyd

Code: Select all

substr($value,0,strlen($value) - 2);

Posted: Mon Sep 06, 2004 7:53 am
by gojira999
feyd wrote:

Code: Select all

substr($value,0,strlen($value) - 2);
Magic! Will try it tonight & let you know.

Thanks Feyd!!! :D