我的代码需要一些帮助。我在解析html标签中的元素时遇到问题,我的html页面中有两个不同的html标签,其中有<a id="link1"
和<a id="streams"
。
当我尝试这个时:
$streams_url = $domdoc->getElementsByTagName('a');
foreach($streams_url as $a)
{
$url[0] = $a->getAttribute("href");
print_r($url);
}
我将从html标记<a id="link1"
和<a id="streams"
中获取完整元素。
这是输出:
Array ( [0] => http://myserverip/get-listing.php?channels=BBC One S East&id=101 )
Array ( [0] => rtmp://www.testserver.com/bbc1 )
Array ( [0] => http://myserverip/get-listing.php?channels=BBC Two&id=102)
Array ( [0] => rtmp://www.testserver.com/bbc2 )
Array ( [0] => http://myserverip/get-listing.php?channels=ITV&id=103 )
Array ( [0] => rtmp://www.testserver.com/itv1 )
Array ( [0] => http://myserverip/get-listing.php?channels=Channel 4&id=104 )
Array ( [0] => rtmp://www.testserver.com/channel4 )
Array ( [0] => http://myserverip/get-listing.php?channels=Channel 5&id=105 )
Array ( [0] => rtmp://www.testserver.com/channel5 )
Array ( [0] => http://myserverip/get-listing.php?channels=Sky One&id=106 )
Array ( [0] => rtmp://www.testserver.com/skyone )
Array ( [0] => http://myserverip/get-listing.php?channels=Sky Living&id=107 )
Array ( [0] => rtmp://www.testserver.com/skyliving )
Array ( [0] => http://myserverip/get-listing.php?channels=Sky Atlantic&id=108 )
Array ( [0] => rtmp://www.testserver.com/skyatlantic )
以下是完整代码:
<?php
ini_set('max_execution_time', 300);
//error_reporting(0);
$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);
$streams_url = $domdoc->getElementsByTagName('a');
foreach($streams_url as $a)
{
$url[0] = $a->getAttribute("href");
print_r($url);
}
?>
我想要实现的是我想从流标签中获取元素,使其显示如下:
Array ( [0] => rtmp://www.testserver.com/bbc1 )
Array ( [0] => rtmp://www.testserver.com/bbc2 )
Array ( [0] => rtmp://www.testserver.com/itv1 )
Array ( [0] => rtmp://www.testserver.com/channel4 )
Array ( [0] => rtmp://www.testserver.com/channel5 )
Array ( [0] => rtmp://www.testserver.com/skyone )
Array ( [0] => rtmp://www.testserver.com/skyliving )
Array ( [0] => rtmp://www.testserver.com/skyatlantic )
你能否告诉我一个例子,我可以用什么方法来解析名为<a id="streams
的标签中的元素,同时忽略其他标签中的元素?
答案 0 :(得分:0)
如果您确定href
的流始终以rtmp://
开头,则可以使用此功能(如果您正在寻找快速简便的解决方案):
foreach($streams_url as $a)
{
$url[0] = $a->getAttribute("href");
if($url[0][0]=='r') print_r($url);
}