I have strings that are formatted as below:
“Houston, TX” – str1
“Chicago, IL” – str2
“Seattle, WA” – str3
i want to extract “TX”, “IL”, “WA” when given each of the above str1/str2/str3, if a state code exists in string (i.e. 2 upper case letters at end of string) using PHP & regex.. any pointers.. i am unable to reliably extract this info from all the strings given to my method.
You don’t need to use regular expressions for this. Assuming the state code can only appear at the very end of a string, you can use this small function: