Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

substr() question

Status
Not open for further replies.

benrob82

Programmer
Jul 28, 2005
116
0
0
GB
hi

I am trying to remove the last two digits from a postcode obviously this can be xxxx xxx or xxx xxx

is the best way to remove the last two digits using substr() function and if so how do i do it to remove the last two digits?

Thanks in advance
 
substr is fine for this.

to get the last two digits:
Code:
$postcode = "SW1Y 7GH";
echo "last two digits are: " . substr($postcode, -1, 2);

to get all digits other than the last two digits
Code:
$postcode = "SW1Y 7GH";
echo "all but last two digits are: " .substr($postcode, 0, strlen($postcode) - 2);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top