如何使用PHP来抓取这些内容,并且只获取lat/long


How to use PHP to scrape this content and only get the lat/long

比方说,我有这个代码:

var i = new GIcon(G_DEFAULT_ICON);
var latlng = new GLatLng(33.013489, -117.140167);
map.setCenter(latlng, 9);
map.panTo(latlng);
var i = new GIcon(G_DEFAULT_ICON);
var latlng = new GLatLng(32.979286, -117.02623);
map.setCenter(latlng, 9);
map.panTo(latlng);
var i = new GIcon(G_DEFAULT_ICON);
var latlng = new GLatLng(32.746319, -117.141449);
map.setCenter(latlng, 9);
map.panTo(latlng);
var i = new GIcon(G_DEFAULT_ICON);
var latlng = new GLatLng(32.758286, -117.125053);
map.setCenter(latlng, 9);
map.panTo(latlng);

如何使用PHP返回列表或仅返回纬度和经度。所以GLatLng() 之间的任何东西

所以我最终会得到一个数组,看起来像这样:

32.758286, -117.125053
32.979286, -117.02623
32.746319, -117.141449
32.758286, -117.125053
preg_match_all('~
    (?<='()            (?# starts with opening parentheses )
    (?P<lat>['d.-]+)   (?# then a digit, dot or dash more than one time )
    's*,'s*            (?# followed by a comma with any number of spaces around )
    (?P<lng>['d.-]+)   (?# then another digit, dot or dash more than one time )
    (?='))             (?# ends with closing parentheses )
~x', $str, $matches);
var_dump($matches);

您可以先找到命名为GLatLng(的开始字符串,然后找到结束字符串),并取两者之间的值。只要你能找到起始字符串,就重复。

演示:

$result = [];
$start  = 'GLatLng(';
$offset = 0;
while (false !== $pos = strpos($buffer, $start, $offset))
{
    $offset   = $pos + strlen($start);
    $result[] = substr($buffer, $offset, strpos($buffer, ')', $offset) - $offset);
}

类似这样的东西:

/* Split the raw input by its newline characters */
$input = explode("'r'n", $rawInput);
$outputStack = array();
foreach ($input as $inputRow)
{
    splitRow = explode("new GLatLng(", $inputRow);
    if (sizeof(splitRow) > 1)
    {
        /* if you can split the row into more than one peice, you know what's going to be in splitRow[1]: your lat, long */
        array_push($output_stack, str_replace(");", "", splitRow[1]));
    }
}