Reputation: 13279
I'm trying to extract two pieces of information from a location.
An example location would be: Hocking Parade, Sorrento WA 6020
Where I'm trying to extract "Sorrento" and "6020".
"Hocking Parade" is a street that I don't need, which will always be followed by a comma. "WA" is a constant. The post code is 4 digits.
EDIT: Should clarify the date is actually in the format:
<span>Hocking Parade, Sorrento WA 6020</span>
Works:
preg_match('/^.*, (.*) [A-Z]{2} (\d{4})$/','Hocking Parade, Sorrento WA 6020',$uTitle);
Doesn't work:
preg_match('/^.*, (.*) [A-Z]{2} (\d{4})$/','<span>Hocking Parade, Sorrento WA 6020</span>',$uTitle);
Upvotes: 1
Views: 470
Reputation: 91385
how about:
$str = "<span>Hocking Parade, Sorrento WA 6020</span>";
preg_match('/,\s+(.*)\s+[A-Z]{2}\s+(\d{4})/', $str, $m);
echo $m[1]; // prints Sorrento
echo $m[2]; // prints 6020
Upvotes: 0
Reputation: 17427
Try this:
<?
$target = '<span>Hocking Parade, Sorrento WA 6020</span>';
preg_match("/.+?,\s+([^\s]+).+?([^<]+)/", $target, $matched);
echo '<pre>'; print_r($matched); echo '</pre>';
?>
Output:
Array
(
[0] => Hocking Parade, Sorrento WA 6020
[1] => Sorrento
[2] => WA 6020
)
Upvotes: 0
Reputation: 98901
This is what you need:
$address= "Hocking Parade, Sorrento WA 6020";
preg_match_all(', (.*?) WA (\d{4})/i', $address, $result, PREG_PATTERN_ORDER);
$city = $result[1][0];
$zip = $result[2][0];
cheers,
Upvotes: 1