May 5, 2006 #1 benrob82 Programmer Joined Jul 28, 2005 Messages 116 Location 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
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
May 5, 2006 #2 jpadie Technical User Joined Nov 24, 2003 Messages 10,094 Location FR 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); Upvote 0 Downvote
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);