Page 1 of 1

Problems emoving the last three letters of a string

Posted: Mon Jul 18, 2005 5:22 am
by icarpenter
Hi can anyone help!

I am trying to remove the last three letters of a string ie: EC2A 1PQ becomes EC2A. but sometimes the postcode will be a different length ie: CT1 4PQ that could also be CT14PQ...So would need to be removed from right side of string.

Has anyone any ideas...

Rgds Ian

Posted: Mon Jul 18, 2005 5:30 am
by timvw
Have a look at the examples with negative offset at http://www.php.net/substr..


Regular expression wise "#\w{3}$#"

Posted: Mon Jul 18, 2005 5:54 am
by harrisonad

Code: Select all

$str = "EC2A 1PQ";
$aray = explode(' ',$str);
$newstr = $aray[0];

Posted: Mon Jul 18, 2005 6:52 am
by icarpenter
Thanks guys both methods worked well!!!

I used...

Code: Select all

$test = substr("EC2A 1PQ",-0,-3);
echo $test;