0

说,我有这个代码:

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
4

3 回答 3

2
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);
于 2013-08-22T21:35:58.467 回答
1

好吧,您可以通过首先找到您命名的开始字符串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);
}
于 2013-08-22T21:38:03.517 回答
1

像这样的东西:

/* 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]));
    }
}
于 2013-08-22T21:52:32.623 回答