Jump to content

how to find the elements to get it separate


mark107

Recommended Posts

I need some help with my PHP, I'm working on my PHP to parsing the contents from the elements. 

 

I have the ids of `<span id="time1">` and `<span id="title1">` for the elements. I'm trying to parse the `time1`, `time2`, `time3`, `time4`, `time5`, `time6`, `time7`, `time8`, `time9` and `title1`, `title2`, `title3`, `title4`, `title5`, `title6`, `title7`, `title8`, `title9` elements which I'm using `span` as the id. 

 

I will get the input of the `time1`, `time2`, `time3`, `time4`, `time5`, `time6`, `time7`, `time8`, `time9` and `title1`, `title2`, `title3`, `title4`, `title5`, `title6`, `title7`, `title8`, `title9` together which I want to get the list of `time1` to `time9` and `title1` to `title9` in separate.

 

Here is the input:

 



    7:00 PMMelissa & Joey - Born to Run7:30 PMMelissa & Joey - More Than Roommates8:00 PMMelissa & Joey - Accidents Will Happen8:30 PMBaby Daddy - From Here to Paternity9:00 PM


 

 

 

Here is the output of get-listing:

 



    <span id="time1">7:00 PM</span> - <span id="title1">Melissa & Joey -  Born to Run</span><br></br>
    <span id="time2">7:30 PM</span> - <span id="title2">Melissa & Joey -  More Than Roommates</span><br></br>
    <span id="time3">8:00 PM</span> - <span id="title3">Melissa & Joey -  Accidents Will Happen</span><br></br>
    <span id="time4">8:30 PM</span> - <span id="title4">Baby Daddy -  From Here to Paternity</span><br></br>
    <span id="time5">9:00 PM</span>


 

 

 

Here is the code:

 



    <?php
    ini_set('max_execution_time', 300);
    $errmsg_arr = array();
    $errflag = false;
    
    $xml .= '<?xml version="1.0" encoding="UTF-8" ?>';
    $xml .= '
    <tv generator-info-name="www.mysite.com/xmltv">';
    
    $baseUrl = file_get_contents('http://www.mysite.com/get-listing.php');
    
    $domdoc = new DOMDocument();
    $domdoc->strictErrorChecking = false;
    $domdoc->recover=true;
    //@$domdoc->loadHTMLFile($baseUrl);
    @$domdoc->loadHTML($baseUrl);
    
    //$links = $domdoc->getElementsByTagName('test');
    //$links = $domdoc->getElementById('test');
    $links = $domdoc->getElementsByTagName('a');
    
    $data = array();
    foreach($links as $link)
    {  
      //echo $domdoc->saveXML($link);
      if($link->getAttribute('href'))
      {
         if(!$link->hasAttribute('id') || $link->getAttribute('id')!='streams')
         {
           $url = str_replace("rtmp://", "", $link->getAttribute('href'));
           $url = str_replace(" ", "%20", $link->getAttribute('href'));
           //echo $url;
           //echo "<br>";
           $sdoc = new DOMDocument();
           $sdoc->strictErrorChecking = false;
           $sdoc->recover=true;
           @$sdoc->loadHTMLFile($url);
           $spans = $sdoc->getElementsByTagName('span');
           $query = parse_url($url)['query'];
           $channel_split = explode("&", $query)[0];
           $channel = urldecode(explode("=",$channel_split)[1]);
           $id_split = explode("&", $query)[1];
           $my_id = urldecode(explode("=",$id_split)[1]);
           //echo $my_id;
           //echo '<channel id='.$my_id.'>';
           //echo $spans->nodeValue;
    
           $flag=0;
           foreach($spans as $span)
           {
             echo $span->nodeValue;
             $starttime=$span->nodeValue;
             //echo "<programme channel='".$my_id." ".$channel." start='".$starttime."' stop='".$stoptime."'>";
    
    if($flag>0)
    {
      //echo "<programme channel='".$my_id." ".$channel." start='".$starttime."' stop='".$stoptime."'>";
      $stoptime=$starttime;
      $flag=1;
    }
    else
    {
      $stoptime=$starttime;
    }
           }
         }
      }
    }
    ?>


 

Can you please tell me how I can create the arrays for the domdocument to get the element id of `time1` to `time9` to make it separate with `title1` to `title9`?

Link to comment
Share on other sites

Is that really your input with the times blending in with the titles?

 

 

Anyway, in a loop you can create a new array and even associate items within that array

$new_array[] = array("id"=>$my_id, "channel"=>$channel, "start"=>$starttime, "stop"=>$stoptime);

Outside the loop can do whatever need with it.

var_dump($new_array);

Edited by QuickOldCar
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.