xpath dom preg_match

时间:2011-10-01 23:34:30

标签: dom curl

我有这个代码与里面的谷歌地图坐标,我试图获得坐标,但某处是一个问题;代码是:

<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 :(

1 个答案:

答案 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;