[thelist] condensing 4 regexes to 1?
Alex Beston
alex.beston at gmail.com
Sat Aug 5 18:16:01 CDT 2006
Hi evolters
http://www.markbarton.com/blog/articles/FAST-6E8BQF contains a hack to
get the post code out of a gmap's url output and mentions it can be
parsed out. this is a proof of concept for a mashup im making rather
than an actual attempt to rip off google, so bear with me.
below is my attempt but its way too long.
so heres what i did (see code at end):
get this bit:
center: {lat: 57.138801,lng: -2.120481
from the output of http://maps.google.co.uk/maps?q=ab106qr&output=js
then split on the comma and regex the two bits and its done.
i thought a way to condense the code is to bung the
center: {lat: 57.138801
and the
,lng: -2.120481
into cells of the matches array on the first regex thus avoiding a
preg_split. or another way of putting it, is it possible to write one
regex that matches different parts of a string and places the result
into several parts of an array? such that:
(pseudocode)
string = ABCDEFGHIJKL
match BCD and GHI place into matches[0] and matches[1] respectively
anyway, so i have 4 regexes and there ought to be a way to get this
down to 2 regexs or even one.
any ideas?
ta!
Alex
---
code:
<?php
$myurl = "http://maps.google.co.uk/maps?q=ab106qr&output=js";
$myhtml = getURL($myurl);
$pattern = '/center: \{lat: (|-)[0-9]{1,3}\.[0-9]{6},lng:
(|-)[0-9]{1,3}\.[0-9]{6}/';
preg_match($pattern, $myhtml , $matches);
$chars = preg_split('/,/', $matches[0], -1, PREG_SPLIT_OFFSET_CAPTURE);
$pattern = "/(|-)[0-9]{1,3}\.[0-9]{6}/";
preg_match($pattern, $chars[0][0] , $matches);
$latitude = $matches[0];
preg_match($pattern, $chars[1][0] , $matches);
$longitude = $matches[0];
echo "latitude: ".$latitude." longitude: ".$longitude;
function getURL($popped){
$fail = "failed";
$remote = @fopen ($popped, "rb");
if (!$remote){
print($popped." FAILED");
return $fail;
}
else{
$html = "";
do {
$data = fread($remote, 8192);
if (strlen($data) == 0) {
break;
}
$html .= $data;
} while(true);
fclose ($remote);
return $html;
}
}
?>
More information about the thelist
mailing list