Hi I'm trying to match a search string so that if there is us zipcode is at the beginning of the string, return the matches of the zipcode as well as the rest of the string. This is what i have so far
$str = "90210 Beverly Hills, CA";
$res = preg_match('/^((\d{5})(-\d{4})?)\s+(.+?)$/', $str, $matches);
But when I print_r the matches it returns with an extra key for the space.
Array
(
[0] => 90210 Beverly Hills, CA
[1] => 90210
[2] => 90210
[3] =>
[4] => Beverly Hills, CA
)
Is there anyway i can improve the pattern and return matches which is not an empty string? Is there a better pattern for this instance? Also if just a zipcode is given or just a text string, it would return false.