如何找到元素让它分开

时间:2014-05-01 00:13:25

标签: php arrays

我正在使用PHP来解析元素中的内容。

我有<span id="time1"><span id="title1">的ID用于元素。我尝试解析time1time2time3time4time5time6time7time8time9title1title2title3title4title5title6,{我使用title7作为ID的{1}},title8title9个元素。

我将获得spantime1time2time3time4time5,{{1}的输入},time6time7time8time9title1title2title3title4title5title6title7我希望获得title8title9time1time9的列表。分开。

这是输入:

title1

以下是get-listing的输出:

title9

以下是代码:

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

您能否告诉我如何为domdocument创建数组,以便将<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> 的元素ID转换为<?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; } } } } } ?> ,使其与time1分隔为time9

2 个答案:

答案 0 :(得分:0)

我希望它会对你有所帮助,我多年前用过这个......

http://simplehtmldom.sourceforge.net/

答案 1 :(得分:0)

<?php

$data = '<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>';


$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($data);

$xpath = new DOMXpath($doc);
$elements = $xpath->query("*/span[@id='time1']");

print_r($elements);

foreach($elements as $e){
  echo $e->nodeValue;
}

&GT;