I want to remove the the 7th and 8th to last characters in a string.
$string = "Tucson AZ 85718";
I am trying to remove the state abbreviation. I have a bunch of these strings for different zip codes, so I can't just replace "AZ" with ''.
I want to remove the the 7th and 8th to last characters in a string.
$string = "Tucson AZ 85718";
I am trying to remove the state abbreviation. I have a bunch of these strings for different zip codes, so I can't just replace "AZ" with ''.
$string = substr($string, 0, -8) . substr($string, -5);
Demo:
php> $string = "Tucson AZ 85718";
php> echo substr($string, 0, -8) . substr($string, -5);
Tucson 85718
A regex would also do the job. This one would remove any 2-uppercase-character string and the space after it:
$string = preg_replace('/\b[A-Z]{2}\b /', '', $string);