Tim Cullen Tim Cullen - 5 months ago 13
HTML Question

Parsing the elements from one of these tags in arrays

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??

Answer

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