如何编写简单的短代码进行解析

时间:2014-05-03 14:06:31

标签: php

我正在处理我的PHP以获取时间列表。我正在使用DomDocument来获取时间,我想找到一种方法来减少代码,因为我在get-listing.php脚本中有69个时间标记。

如果我使用它:

$time1 = $xpath->query("*/span[@id='time1']");
$time2 = $xpath->query("*/span[@id='time2']");
$time3 = $xpath->query("*/span[@id='time3']");
$time4 = $xpath->query("*/span[@id='time4']");
$time5 = $xpath->query("*/span[@id='time5']");
$time6 = $xpath->query("*/span[@id='time6']");
$time7 = $xpath->query("*/span[@id='time7']");
...etc until to get time69

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

对于我来说,编写解析从time1标记到time69标记的时间的代码列表太大了。

<?php
ini_set('max_execution_time', 300);
$errmsg_arr = array();
$errflag = false;

function getState($string)
{
  $ex = explode(" ",$string);
  return $ex[1];
}

$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('a');
$i = 0;
$count = 0;

$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);
       $time1_span = $sdoc->getElementById('time1');       
       //$spans = $sdoc->getElementsByTagName('time1');
       $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]);
       $xpath = new DOMXpath($sdoc);
       $time1 = $xpath->query("*/span[@id='time1']");
       $time2 = $xpath->query("*/span[@id='time2']");
       $time3 = $xpath->query("*/span[@id='time3']");
       //$time4 = $xpath->query("*/span[@id='time4']");

       $array = array(
         $time1,$time2,$time3
       );

       // Save the output format
       $DATE_FORMAT_STRING = "YmdHis";

       // GET the current STAGE
       $current_state = getState($array[0]->nodeValue);
       $offset = 0;
       $flag = 0;

       foreach($array as $time)
       {
         echo $time->item(0)->nodeValue;
         // Get the item state.  
         //$this_state = getState($time);
         $this_state = getState($time->item(0)->nodeValue);
         //echo $time->nodeValue;

         // check if we past a day? 
         if($current_state == "PM" && $this_state == "AM")
         {
           $offset++;
         }
         $this_unix = strtotime($time->item(0)->nodeValue) + (60 * 60 * 24 * $offset);
         $values[] = date($DATE_FORMAT_STRING, $this_unix);
         //echo date($DATE_FORMAT_STRING, $this_unix);
         echo $values[$count];
         echo "<br></br>";

         $starttime = $times->nodeValue;
         //echo $starttime;
         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;
     }*/ 
         $current_state = $this_state;
         $count++;
       }         
     }
  }
}
?>

我的问题是你如何编写简单的编写代码的方法,使用较少的代码行来获取时间1到时间69的元素id更短?

编辑:当我尝试打印字符串列表时,我遇到了致命的错误。

错误是跳到这一行:

$time{$i} = $xpath->query("*/span[@id='time".$i."']");

可能是这样的:

echo $time->item(0)->nodeValue;

致命错误:不能在第57行的/home/myusername/public_html/work_on_this.php中使用DOMNodeList类型的对象作为数组

以下是更新代码:

for ($i = 1; $i < 70; $i++) 
       {
         $time{$i} = $xpath->query("*/span[@id='time".$i."']");

         $array = array(
           $time{$i}
         );

         foreach($array as $time)
         {
           echo $time->item(0)->nodeValue;
         }
       }

2 个答案:

答案 0 :(得分:0)

使用for循环:

for ($i = 1; $i < 70; $i++) {
    $time{$i} = $xpath->query("*/span[@id = \"time\" . $i]");
}

答案 1 :(得分:0)

将代码更新为以下内容,

$time_arr = array();
for ($i = 1; $i < 70; $i++){
   $time_arr[] = $xpath->query("*/span[@id='time".$i."']");
}

foreach($time_arr as $time){
   echo $time->item(0)->nodeValue;
}

我是直接在for循环中将值添加到数组中,而不是单独分配它们。

并且foreach不应该在for循环中获取时间值。