Jump to content

Parsing elements from href tags


Recommended Posts

Hi guys,
I need some help with my code. I want to parse each element from the streams tags but I cant find out how I could do this.

When I try this:

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

I will get something like this:

    serverip page isn’t working

    serverip is currently unable to handle this request.

    HTTP ERROR 500




Here is the 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();
    foreach($links as $link)
    {
      if($link->getAttribute('href'))
      {
      }
    }
    >?


Here is the html data:

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



Here is what I want to achieve:

    rtmp://www.testserver.com/skyatlantic


I want to parse each element from the streams tags.

Can you please show me how i could do this in PHP??
Link to post
Share on other sites

 

Writing syntactically valid PHP code might be a good start.

 

This isn't valid (note the closing bracket):

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

This isn't either:

>?

 

Sorry I have make a mistake with my code. I can see there is no span tag as I am using the <a id tags so do you know how I could parse the <a id tag to get the element from the href??

 

Here is where you can see it:

 

<a id="streams" href="rtmp://www.testserver.com/skyatlantic">Stream 1</a>
Edited by mark107
Link to post
Share on other sites

So you have fixed the syntax errors and made sure that $xpath is actually defined? What exactly is the problem now? I'm talking about specific error messages or symptoms, not "it doesn't work".

Link to post
Share on other sites

Yes I have fixed the issues and I have forgot to add the xpath to definte it.

 

I want to parse the element from one of these hyperlink.

 

Here there are two different hyperlinks:

<a id="link1" href="http://myserverip/getlisting.php?channel=skyatlantic">http://myserverip/getlisting.php?channel=Sky Atlantic&id=108</a>

<a id="streams" href="rtmp://www.testserver.com/skyatlantic">Stream 1</a>

I want to parse the element from the tag called "streams".

 

When I try this:

$xpath = new DOMXpath($domdoc);

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

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

It will only let me to parse one of these element as I have got more than one element in my html code.

 

Do you know how i could parse each element from each streams tags??

Edited by mark107
Link to post
Share on other sites

Um, what?

 

You said you want to get the href value of exactly one a element with the ID "streams". Your code does that. Now you're talking about more than one element? What are those other elements you want to fetch?

 

If you have more than one element with the same ID, that's wrong. The ID must be unique accross the entire document.

Link to post
Share on other sites

You heard me.

 

Yes of course I do, I want to fetch the elements from the ID "Streams" for each channel. I have got two different href value which there are <a id="link1" and <a id="streams". Each channel I have as it have got the same ID "Streams" but it come with each different link, example: rtmp://www.testserver.com/bbc1, rtmp://www.testserver.com/bbc2...etc.

 

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 ID "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 )

Do you know what is the best way I could use to parse the elements from the tag from the ID "Streams" while ignore the elements from the other tag??

Link to post
Share on other sites

Again: An HTML document cannot have multiple elements with the same ID. This violates both the HTML specification and common logic. ID means “identifier”, so by definition it uniquely references a single entity.

 

So the real question is: Why is your data source fudged up? Where does the broken HTML come from? Did you generate this?

 

Fix the data source, and the rest will be easy.

Link to post
Share on other sites
This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.