I have a bunch of strings in php that all look like this:
10 NE HARRISBURG
4 E HASWELL
2 SE OAKLEY
6 SE REDBIRD
PROVO
6 W EADS
21 N HARRISON
What I am needing to do is remove the numbers and the letters from before the city names. The problem I am having is that it varies a lot from city to city. The data is almost never the same. Is it possible to remove this data and keep it in a separate string?
Check out regular expressions and preg_replace. $nameOfCity = preg_replace("/^\d+\s+\w{1,2}\s+/", "", $source);
Explained:
^
matches the beginning of the string\d+\s+
start with one or more numbers followed by one or more white-space characters\w{1,2}\s+
then there should be one or two letters followed by one or more white-space charactersCases not covered
If you want to be more precise, I assume you could enumerate all the possible letters before the city name (S|SE|E|NE|N|NW|W|SW)
instead of matching any one or two letter long strings.
See if the following works for you:
$new_str = preg_replace('/^([0-9]* \w+ )?(.*)$/', '$2', $str);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With