Problems emoving the last three letters of a string

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
icarpenter
Forum Commoner
Posts: 84
Joined: Mon Mar 07, 2005 8:12 am
Location: Kent, England

Problems emoving the last three letters of a string

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Have a look at the examples with negative offset at http://www.php.net/substr..


Regular expression wise "#\w{3}$#"
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Post by harrisonad »

Code: Select all

$str = "EC2A 1PQ";
$aray = explode(' ',$str);
$newstr = $aray[0];
User avatar
icarpenter
Forum Commoner
Posts: 84
Joined: Mon Mar 07, 2005 8:12 am
Location: Kent, England

Post by icarpenter »

Thanks guys both methods worked well!!!

I used...

Code: Select all

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