如何在向url发送请求时清除字符串

时间:2014-04-25 17:39:23

标签: php

我正在研究我的PHP来生成xml。我正在向每个网址发送请求以获取小时数字符串,因此我可以将其转换为格式日期,如20140425130000,但我遇到了循环问题。当我将小时数转换为格式化日期时,对于每种格式我都将它放在每个start =“”中,它应该有不同的输入,但我在每个start =“”中都有相同的输入。我曾经尝试使用$ html_two-> clear()来清除每次向每个网址发送请求时的格式,因此我会一次输入不同的格式,而不会一遍又一遍地获得相同的输入。 / p>

这是输入格式:

20140425130000
20140425130000
20140425125800

这是xml输出:

<tv generator-info-name="www.mysite.com/xmltv">
<channel id="101 ABC FAMILY">
<display-name>101 ABC FAMILY</display-name>
<programme channel="101 ABC FAMILY" start="20140425130000" stop="">
<title lang="en"></title>
<sub-title lang="en"></sub-title>
<desc lang="en"></desc>
<category lang="en"></category>
</programme>
</channel>

<channel id="102 Animal Planet">
<display-name>102 Animal Planet</display-name>
<programme channel="107 Animal Planet" start="20140425130000" stop="">
<title lang="en"></title>
<sub-title lang="en"></sub-title>
<desc lang="en"></desc>
<category lang="en"></category>
</programme>
</channel>

<channel id="111 BRAVO USA">
<display-name>111 BRAVO USA</display-name>
<programme channel="111 BRAVO USA" start="20140425130000" stop="">
<title lang="en"></title>
<sub-title lang="en"></sub-title>
<desc lang="en"></desc>
<category lang="en"></category>
</programme>
</channel>
</tv>

以下是它应该是什么样的:

<tv generator-info-name="www.mysite.com/xmltv">
<channel id="101 ABC FAMILY">
<display-name>101 ABC FAMILY</display-name>
<programme channel="101 ABC FAMILY" start="20140425130000" stop="">
<title lang="en"></title>
<sub-title lang="en"></sub-title>
<desc lang="en"></desc>
<category lang="en"></category>
</programme>
</channel>

<channel id="102 Animal Planet">
<display-name>102 Animal Planet</display-name>
<programme channel="107 Animal Planet" start="20140425130000" stop="">
<title lang="en"></title>
<sub-title lang="en"></sub-title>
<desc lang="en"></desc>
<category lang="en"></category>
</programme>
</channel>

<channel id="111 BRAVO USA">
<display-name>111 BRAVO USA</display-name>
<programme channel="111 BRAVO USA" start="20140425125800" stop="">
<title lang="en"></title>
<sub-title lang="en"></sub-title>
<desc lang="en"></desc>
<category lang="en"></category>
</programme>
</channel>
</tv>

这是PHP:

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

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">';
$base1 = "http://www.mysite.com/get-listing.php";
$html = file_get_html($base1);      

foreach($html->find('p[id=links]') as $element)
{
  $program_list[ $count ] = array();
  $id_split = explode("?", $element->plaintext);
  $id_split = explode("&", $link_split[1]);
  $channels = explode("channels=",$id_split[0]);
  $channels = $channels[1];
  $id = explode("id=",$id_split[1]);
  $id = $id[1];

  //channels
  //$channel_test = $html->find('p[id=channels]', 10)->plaintext;
  //echo $channel_test;

  $program_list[ $count ]['channels'] = $channels;
  $program_list[ $count ]['id'] = $id;

  $channels_split = explode("?", $element->plaintext);
  $channels_split = explode("&", $channels_split[1]);
  $channel_id = explode("channels=",$channels_split[0]);
  $channel_id = $channel_id[1];
  $my_id = explode("id=",$channels_split[1]);
  $my_id = $my_id[1];

  $channel = urlencode($channel_id);
  $id_1 = urlencode($my_id);
  $html_two = file_get_html("http://www.mysite.com/get-listing.php?channels=" . $channel . "&id=" . $my_id);
  $time1 = $html_two->find('span[id=time1]',0)->plaintext;
  $time2 = $html_two->find('span[id=time2]',0)->plaintext;
  $time3 = $html_two->find('span[id=time3]',0)->plaintext;
  $time4 = $html_two->find('span[id=time4]',0)->plaintext;
  $time5 = $html_two->find('span[id=time5]',0)->plaintext;
  $time6 = $html_two->find('span[id=time6]',0)->plaintext;
  $time7 = $html_two->find('span[id=time7]',0)->plaintext;
  $time8 = $html_two->find('span[id=time8]',0)->plaintext;
  $time9 = $html_two->find('span[id=time9]',0)->plaintext;
  $time10 = $html_two->find('span[id=time10]',0)->plaintext;
  $time11 = $html_two->find('span[id=time11]',0)->plaintext;
  $time12 = $html_two->find('span[id=time12]',0)->plaintext;
  $time13 = $html_two->find('span[id=time13]',0)->plaintext;
  $time14 = $html_two->find('span[id=time14]',0)->plaintext;
  $time15 = $html_two->find('span[id=time15]',0)->plaintext;
  $time16 = $html_two->find('span[id=time16]',0)->plaintext;
  $time17 = $html_two->find('span[id=time17]',0)->plaintext;


  $array = array(
    $time1,
    $time2,
    $time3,
    $time4,
    $time5,
    $time6,
    $time7,
    $time8,
    $time9,
    $time10,
    $time11,
    $time12,
    $time13,
    $time14,
    $time15,
    $time16,
    $time17,
  );

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

  // GET the current STAGE
  $current_state = getState($array[0]);
  $offset = 0;
  $time_1 = array();
  $time_2 = array();
  $time_3 = array();
  $time_4 = array();
  $time_5 = array();
  $time_6 = array();
  $time_7 = array();
  $time_8 = array();         
  $time_9 = array();
  $time_10 = array();  

  foreach($array as $time)
  {
    // Get the item state.  
    $this_state = getState($time);

    // check if we past a day? 
    if($current_state == "PM" && $this_state == "AM")
    {
      $offset++;
    }
    $this_unix1 = strtotime($time) + (60 * 60 * 24 * $offset);
    $this_unix2 = strtotime($time) + (60 * 60 * 24 * $offset);
    $this_unix3 = strtotime($time) + (60 * 60 * 24 * $offset);
    $this_unix4 = strtotime($time) + (60 * 60 * 24 * $offset);
    $this_unix5 = strtotime($time) + (60 * 60 * 24 * $offset);
    $this_unix6 = strtotime($time) + (60 * 60 * 24 * $offset);
    $this_unix7 = strtotime($time) + (60 * 60 * 24 * $offset);
    $this_unix8 = strtotime($time) + (60 * 60 * 24 * $offset);
    $this_unix9 = strtotime($time) + (60 * 60 * 24 * $offset);
    $this_unix10 = strtotime($time) + (60 * 60 * 24 * $offset);

    $time_1[] = date($DATE_FORMAT_STRING, $this_unix1);
    $time_2[] = date($DATE_FORMAT_STRING, $this_unix2);
    $time_3[] = date($DATE_FORMAT_STRING, $this_unix3);
    $time_4[] = date($DATE_FORMAT_STRING, $this_unix4);
    $time_5[] = date($DATE_FORMAT_STRING, $this_unix5);
    $time_6[] = date($DATE_FORMAT_STRING, $this_unix6);
    $time_7[] = date($DATE_FORMAT_STRING, $this_unix7);
    $time_8[] = date($DATE_FORMAT_STRING, $this_unix8);
    $time_9[] = date($DATE_FORMAT_STRING, $this_unix9);
    $time_10[] = date($DATE_FORMAT_STRING, $this_unix10);
  }
  $program_list[$count]['start_time1'] = $time_1[0];
  $count++;
  $current_state = $this_state;


  if (strpos($channel, '+') !== false)
  {
    $channel = str_replace('+', ' ', $channel);
  }

  //program 1#
  $xml .= "
  <channel id='" . $my_id. " " . $channel . "'>";
  $xml .= "
    <display-name>" . $my_id. " " . $channel; 
  $xml .= "</display-name>";
  $xml .= "
  <programme channel='" . $my_id. " " . $channel . "' start='" . $program_list[$i]['start_time1'] . "' stop='" . $program_list[$i]['end_time1'] . "'>";
  $xml .= '
    <title lang="en">';
  $xml .= ' </title>';
  $xml .= '
    <sub-title lang="en">';
  $xml .= ' </sub-title>';
  $xml .= '
    <desc lang="en">';
  $xml .= ' </desc>';
  $xml .= '
    <category lang="en">';
  $xml .= ' </category>';
  $xml .= "
  </programme>";
  $xml .= '
  </channel>';
}

//Clear DOM object
$html_two->clear();
unset($html_two);

$xml .= '
</tv>';
//output the xml to the browser, write $xml to a file here...
header("Content-Type: text/xml");
echo $xml;
$handle = fopen("myChannel.xml", "w"); 
fwrite ($handle, $xml);
?>

当我将请求发送到url以便一次获取格式时,是否有人知道如何在每次输入格式时获取每种不同的格式输入,因此当我输入格式时我不会得到相同的格式输入在每个开始=“”?

编辑:它将开始在每个start =“”中显示输入格式,但在xml的开头,它将显示为空输入,您可以看到:

<tv generator-info-name="www.mysite.com/xmltv">
<channel id="101 ABC FAMILY">
<display-name>101 ABC FAMILY</display-name>
<programme channel="101 ABC FAMILY" start="" stop="">
<title lang="en"></title>
<sub-title lang="en"></sub-title>
<desc lang="en"></desc>
<category lang="en"></category>
</programme>
</channel>
<channel id="102 CBS">
<display-name>102 CBS</display-name>
<programme channel="102 CBS" start="20140425140000" stop="">
<title lang="en"></title>
<sub-title lang="en"></sub-title>
<desc lang="en"></desc>
<category lang="en"></category>
</programme>
</channel>
<channel id="103 CNN USA">
<display-name>103 CNN USA</display-name>
<programme channel="103 CNN USA" start="20140425140000" stop="">
<title lang="en"></title>
<sub-title lang="en"></sub-title>
<desc lang="en"></desc>
<category lang="en"></category>
</programme>
</channel>
<channel id="105 ESPN USA">
<display-name>105 ESPN USA</display-name>
<programme channel="105 ESPN USA" start="20140425140000" stop="">
<title lang="en"></title>
<sub-title lang="en"></sub-title>
<desc lang="en"></desc>
<category lang="en"></category>
</programme>
</channel>
<channel id="106 Fox News">
<display-name>106 Fox News</display-name>
<programme channel="106 Fox News" start="20140425150000" stop="">
<title lang="en"></title>
<sub-title lang="en"></sub-title>
<desc lang="en"></desc>
<category lang="en"></category>
</programme>
</channel>
</tv>

以下是更新代码:

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

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">';
$base1 = "http://www.mysite.com/get-listing.php";
$html = file_get_html($base1);      
$i = 0;

foreach($html->find('p[id=links]') as $element)
{
  $program_list[ $count ] = array();
  $id_split = explode("?", $element->plaintext);
  $id_split = explode("&", $link_split[1]);
  $channels = explode("channels=",$id_split[0]);
  $channels = $channels[1];
  $id = explode("id=",$id_split[1]);
  $id = $id[1];

  //channels
  //$channel_test = $html->find('p[id=channels]', 10)->plaintext;
  //echo $channel_test;

  $program_list[ $count ]['channels'] = $channels;
  $program_list[ $count ]['id'] = $id;

  $channels_split = explode("?", $element->plaintext);
  $channels_split = explode("&", $channels_split[1]);
  $channel_id = explode("channels=",$channels_split[0]);
  $channel_id = $channel_id[1];
  $my_id = explode("id=",$channels_split[1]);
  $my_id = $my_id[1];

  $channel = urlencode($channel_id);
  $id_1 = urlencode($my_id);
  $html_two = file_get_html("http://www.mysite.com/get-listing.php?channels=" . $channel . "&id=" . $my_id);
  $time1 = $html_two->find('span[id=time1]',0)->plaintext;
  $time2 = $html_two->find('span[id=time2]',0)->plaintext;
  $time3 = $html_two->find('span[id=time3]',0)->plaintext;
  $time4 = $html_two->find('span[id=time4]',0)->plaintext;
  $time5 = $html_two->find('span[id=time5]',0)->plaintext;
  $time6 = $html_two->find('span[id=time6]',0)->plaintext;
  $time7 = $html_two->find('span[id=time7]',0)->plaintext;
  $time8 = $html_two->find('span[id=time8]',0)->plaintext;
  $time9 = $html_two->find('span[id=time9]',0)->plaintext;
  $time10 = $html_two->find('span[id=time10]',0)->plaintext;
  $time11 = $html_two->find('span[id=time11]',0)->plaintext;
  $time12 = $html_two->find('span[id=time12]',0)->plaintext;
  $time13 = $html_two->find('span[id=time13]',0)->plaintext;
  $time14 = $html_two->find('span[id=time14]',0)->plaintext;
  $time15 = $html_two->find('span[id=time15]',0)->plaintext;
  $time16 = $html_two->find('span[id=time16]',0)->plaintext;
  $time17 = $html_two->find('span[id=time17]',0)->plaintext;


  $array = array(
    $time1,
    $time2,
    $time3,
    $time4,
    $time5,
    $time6,
    $time7,
    $time8,
    $time9,
    $time10,
    $time11,
    $time12,
    $time13,
    $time14,
    $time15,
    $time16,
    $time17,
  );

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

  // GET the current STAGE
  $current_state = getState($array[0]);
  $offset = 0;
  $time_1 = array();
  $time_2 = array();
  $time_3 = array();
  $time_4 = array();
  $time_5 = array();
  $time_6 = array();
  $time_7 = array();
  $time_8 = array();         
  $time_9 = array();
  $time_10 = array();  

  foreach($array as $time)
  {
    // Get the item state.  
    $this_state = getState($time);

    // check if we past a day? 
    if($current_state == "PM" && $this_state == "AM")
    {
      $offset++;
    }
    $this_unix1 = strtotime($time) + (60 * 60 * 24 * $offset);
    $this_unix2 = strtotime($time) + (60 * 60 * 24 * $offset);
    $this_unix3 = strtotime($time) + (60 * 60 * 24 * $offset);
    $this_unix4 = strtotime($time) + (60 * 60 * 24 * $offset);
    $this_unix5 = strtotime($time) + (60 * 60 * 24 * $offset);
    $this_unix6 = strtotime($time) + (60 * 60 * 24 * $offset);
    $this_unix7 = strtotime($time) + (60 * 60 * 24 * $offset);
    $this_unix8 = strtotime($time) + (60 * 60 * 24 * $offset);
    $this_unix9 = strtotime($time) + (60 * 60 * 24 * $offset);
    $this_unix10 = strtotime($time) + (60 * 60 * 24 * $offset);

    $time_1[] = date($DATE_FORMAT_STRING, $this_unix1);
    $time_2[] = date($DATE_FORMAT_STRING, $this_unix2);
    $time_3[] = date($DATE_FORMAT_STRING, $this_unix3);
    $time_4[] = date($DATE_FORMAT_STRING, $this_unix4);
    $time_5[] = date($DATE_FORMAT_STRING, $this_unix5);
    $time_6[] = date($DATE_FORMAT_STRING, $this_unix6);
    $time_7[] = date($DATE_FORMAT_STRING, $this_unix7);
    $time_8[] = date($DATE_FORMAT_STRING, $this_unix8);
    $time_9[] = date($DATE_FORMAT_STRING, $this_unix9);
    $time_10[] = date($DATE_FORMAT_STRING, $this_unix10);
  }
  $program_list[$count]['start_time1'] = $time_1[0];
  $count++;
  $current_state = $this_state;

  if (strpos($channel, '+') !== false)
  {
    $channel = str_replace('+', ' ', $channel);
  }

  //program 1#
  $xml .= "
  <channel id='" . $my_id. " " . $channel . "'>";
  $xml .= "
    <display-name>" . $my_id. " " . $channel; 
  $xml .= "</display-name>";
  $xml .= "
  <programme channel='" . $my_id. " " . $channel . "' start='" . $program_list[$i]['start_time1'] . "' stop='" . $program_list[$i]['end_time1'] . "'>";
  $xml .= '
    <title lang="en">';
  $xml .= ' </title>';
  $xml .= '
    <sub-title lang="en">';
  $xml .= ' </sub-title>';
  $xml .= '
    <desc lang="en">';
  $xml .= ' </desc>';
  $xml .= '
    <category lang="en">';
  $xml .= ' </category>';
  $xml .= "
  </programme>";
  $xml .= '
  </channel>';
  $i++;
}

//Clear DOM object
$html->clear();
unset($html);

$xml .= '
</tv>';
//output the xml to the browser, write $xml to a file here...
header("Content-Type: text/xml");
echo $xml;
$handle = fopen("myChannel.xml", "w"); 
fwrite ($handle, $xml);
?>

1 个答案:

答案 0 :(得分:1)

$i = 0;

foreach($html->find('p[id=links]') as $element)
{
    // your init codes

    // program 1#
    // your xml

    // at the end of the loop, increase $i by 1
    $i++;

}

否则,$i在每个循环中都会保持相同的值,但您不想一遍又一遍地打印相同的start_time1