从href标签中解析元素

时间:2016-06-18 19:28:56

标签: php html xpath

我的代码需要一些帮助。我想从流标签中解析每个元素,但我无法找到如何做到这一点。

当我尝试这个时:

$streams_url = $xpath->query("//span[@id='streams'"]);

我会得到这样的东西:

serverip page isn’t working

serverip is currently unable to handle this request.

HTTP ERROR 500

这是php:

<?php
ini_set('max_execution_time', 300);
$errmsg_arr = array();
$errflag = false;

function getState($string)
{
  $ex = explode(" ",$string);
  return $ex[1];
}

$baseUrl = file_get_contents('http://myserverip/get-listing.php');

$domdoc = new DOMDocument();
$domdoc->strictErrorChecking = false;
$domdoc->recover=true;
@$domdoc->loadHTML($baseUrl);
$links = $domdoc->getElementsByTagName('a');
$i = 0;
$count = 0;
$streams_url = $xpath->query("//span[@id='streams'"]);
echo $streams_url;

$data = array();
>?

这是html数据:

<a id="link1" href="http://myserverip/getlisting.php?channel=skyatlantic">http://myserverip/getlisting.php?channel=Sky Atlantic&id=108</a><br><br><a id="streams" href="rtmp://www.testserver.com/skyatlantic">Stream 1</a>

这是我想要实现的目标:

http://www.testserver.com/stream01

我想从stream标签中解析每个元素。

你能告诉我如何用PHP做到这一点吗?

1 个答案:

答案 0 :(得分:0)

由于您正在寻找id,因此您实际上并不需要使用XPath。这将完成这项工作:

$el = $domdoc->getElementById('streams');
$url = $el->getAttribute('href');

在评论中提到您有重复的id值:这是无效的HTML。但您可以按如下方式处理它们:

$streams_url = $xpath->query("//*[@id='streams']");
foreach($streams_url as $a) {
    $url[] = $a->getAttribute("href");
}
print_r($url); // array of href values
相关问题