Ditching characters in a value

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gojira999
Forum Newbie
Posts: 5
Joined: Fri Sep 03, 2004 7:42 am
Location: Monster Island

Ditching characters in a value

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

[php_man]substr[/php_man]
gojira999
Forum Newbie
Posts: 5
Joined: Fri Sep 03, 2004 7:42 am
Location: Monster Island

Post 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);
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

substr($value,0,strlen($value) - 2);
gojira999
Forum Newbie
Posts: 5
Joined: Fri Sep 03, 2004 7:42 am
Location: Monster Island

Post by gojira999 »

feyd wrote:

Code: Select all

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

Thanks Feyd!!! :D
Post Reply