dongliao3450
2016-06-18 22:11
浏览 64
已采纳

从数组中的其中一个标记中解析元素

I need some help with my code. I have a problem with parsing the elements from the html tags, there are two different html tags I have in my html page which there are <a id="link1" and <a id="streams".

When I try this:

$streams_url = $domdoc->getElementsByTagName('a');

foreach($streams_url as $a)
{
   $url[0] = $a->getAttribute("href");
   print_r($url);
}

I will get the full elements from the both html tags <a id="link1" and <a id="streams".

Here is the output:

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 )

Here is the full code:

<?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);
}
?>

What I want to achieve is I want to get the elements from the streams tags to make it show like this:

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 )

Could you please show me an example of what the best way I could use to parse the elements from the tag called <a id="streams while ignore the elements from the other tag??

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongzhimin2231 2016-06-18 22:51
    已采纳

    If you're sure that href of stream will always begin with rtmp:// you can use this (if you are looking for quick and easy solution):

    foreach($streams_url as $a)
    {
       $url[0] = $a->getAttribute("href");
       if($url[0][0]=='r') print_r($url);
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题