2011-10-01 62 views
0

我有谷歌地圖座標裏面的代碼,我嘗試獲取座標,但某處是一個問題;該代碼是:xpath dom preg_match

<iframe width="430" scrolling="no" height="250" frameborder="0" src="http://maps.google.cz/maps/ms?msa=0&hl=cs&brcurrent=5,0,0&ie=UTF8&vpsrc=6&msid=207589766138527801127.0004aadb2c99231cecabd&ll=44.782627,20.48152&spn=0.003808,0.009205&z=16&output=embed" marginwidth="0" marginheight="0"/> 

,我試圖讓座標與此代碼:

$string = curl($e->textContent); 

    preg_match('#&ll=(.*?)&#is', $string, $matches); 
    list($lat, $lng) = explode(',', $matches[1]); 
    $data['lat'] = $lat; 
    $data['lng'] = $lng; 

,但不工作!

問題在哪裏?我錯在哪裏! (對不起,英語)


這是我的全部代碼,但不工作:

function curl($url){ 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_ENCODING, "gzip"); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
    $data = curl_exec($ch); 
    curl_close($ch); 
    return $data; 
} 

libxml_use_internal_errors(true); 



$dom = new DOMDocument(); 
@$dom->loadHTMLFile('http://www.kupoman.rs/aktivne-ponude/'); 
$xpath = new DOMXPath($dom); 
$entries = $xpath->query("//div[@class='dealcontent']/h1/a/@href"); 
$output = array(); 
$i = 1; 
foreach($entries as $e) { 
    $dom2 = new DOMDocument(); 
    @$dom2->loadHTMLFile($e->textContent); 
    $xpath2 = new DOMXPath($dom2); 
    $data = array(); 
$string = $xpath2->query("//iframe/@src")->item(0)->textContent; 
$data['link']= ($e->textContent); 
    $data['naslov'] = trim($xpath2->query("//div[@class='dealcontent']/h1")->item(0)->textContent); 
    $data['slika'] = trim($xpath2->query("//div[@class='slideshow']/img[1]/@src")->item(0)->textContent); 

    preg_match('/.*&ll=([\d.,]+)&.*/', $string, $matches); 
list($lat, $lng) = explode(',', $matches[1]); 
$data['lat'] = $lat; 
$data['lng'] = $lng; 

一切都很好,但座標是0,0 :(

回答

0

有很多錯誤的有沒有xpath表達式,你的正則表達式是錯誤的,你正在調用curl出於某種原因。也許你正在嘗試這樣做:

$dom = new DOMDocument(); 
@$dom->loadHTML('<iframe width="430" scrolling="no" height="250" frameborder="0" src="http://maps.google.cz/maps/ms?msa=0&hl=cs&brcurrent=5,0,0&ie=UTF8&vpsrc=6&msid=207589766138527801127.0004aadb2c99231cecabd&ll=44.782627,20.48152&spn=0.003808,0.009205&z=16&output=embed" marginwidth="0" marginheight="0"/>'); 

$xpath = new DOMXPath($dom); 
$string = $xpath->query("//iframe/@src")->item(0)->textContent; 

preg_match('/.*&ll=([\d.,]+)&.*/', $string, $matches); 
list($lat, $lng) = explode(',', $matches[1]); 
$data['lat'] = $lat; 
$data['lng'] = $lng;