我正在使用我的PHP来使用domdocument获取字符串列表。当我遇到字符串问题时,我需要你的帮助。
当我获得输入的字符串列表时,当我在PHP上输出它时,我会一遍又一遍地得到相同的字符串。
您可以在输出中看到我保持输出相同字符串的位置:
<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="20140507180000" stop="
20140507183000">
<title lang="en">Reba - Meet the Parents</title>
<sub-title lang="en"></sub-title>
<desc lang="en"></desc>
<category lang="en"></category>
</programme>
<programme channel="101 ABC FAMILY" start="20140507183000" stop="
20140507190000">
<title lang="en">Reba - Meet the Parents</title>
<sub-title lang="en"></sub-title>
<desc lang="en"></desc>
<category lang="en"></category>
</programme>
<programme channel="101 ABC FAMILY" start="20140507190000" stop="
20140507193000">
<title lang="en">Reba - Meet the Parents</title>
<sub-title lang="en"></sub-title>
<desc lang="en"></desc>
<category lang="en"></category>
</programme>
<programme channel="101 ABC FAMILY" start="20140507193000" stop="
20140507200000">
<title lang="en">Reba - Meet the Parents</title>
<sub-title lang="en"></sub-title>
<desc lang="en"></desc>
<category lang="en"></category>
</programme>
<programme channel="101 ABC FAMILY" start="20140507200000" stop="
20140507203000">
<title lang="en">Reba - Meet the Parents</title>
<sub-title lang="en"></sub-title>
<desc lang="en"></desc>
<category lang="en"></category>
</programme>
<programme channel="101 ABC FAMILY" start="20140507203000" stop="
20140507210000">
<title lang="en">Reba - Meet the Parents</title>
<sub-title lang="en"></sub-title>
<desc lang="en"></desc>
<category lang="en"></category>
</programme>
</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="20140507180000" stop="
20140507183000">
<title lang="en">he Middle - The Telling</title>
<sub-title lang="en"></sub-title>
<desc lang="en"></desc>
<category lang="en"></category>
</programme>
<programme channel="101 ABC FAMILY" start="20140507183000" stop="
20140507190000">
<title lang="en">he Middle - The Wedding</title>
<sub-title lang="en"></sub-title>
<desc lang="en"></desc>
<category lang="en"></category>
</programme>
<programme channel="101 ABC FAMILY" start="20140507190000" stop="
20140507193000">
<title lang="en">Melissa & Joey - More Than Roommates</title>
<sub-title lang="en"></sub-title>
<desc lang="en"></desc>
<category lang="en"></category>
</programme>
<programme channel="101 ABC FAMILY" start="20140507193000" stop="
20140507200000">
<title lang="en">Melissa & Joey - Accidents Will Happen</title>
<sub-title lang="en"></sub-title>
<desc lang="en"></desc>
<category lang="en"></category>
</programme>
<programme channel="101 ABC FAMILY" start="20140507200000" stop="
20140507203000">
<title lang="en">Melissa & Joey - Right Time, Right Place</title>
<sub-title lang="en"></sub-title>
<desc lang="en"></desc>
<category lang="en"></category>
</programme>
<programme channel="101 ABC FAMILY" start="20140507203000" stop="
20140507210000">
<title lang="en">Baby Daddy - Curious Georgie</title>
<sub-title lang="en"></sub-title>
<desc lang="en"></desc>
<category lang="en"></category>
</programme>
</tv>
以下是输入的字符串列表:
The Middle - The Telling
The Middle - The Wedding
Melissa & Joey - More Than Roommates
Melissa & Joey - Accidents Will Happen
Melissa & Joey - Right Time, Right Place
Baby Daddy - Curious Georgie
John Tucker Must Die(D,L,V,S)
The 700 Club
Baby Daddy - From Here to Paternity
Reba - Meet the Parents
这是PHP:
<?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.myownsite.com/get-listing.php'
);
$domdoc = new DOMDocument();
$domdoc->strictErrorChecking = false;
$domdoc->recover=true;
@$domdoc->loadHTML($baseUrl);
$links = $domdoc->getElementsByTagName('a');
$i = 0;
$count = 0;
$data = array();
foreach($links as $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'));
$sdoc = new DOMDocument();
$sdoc->strictErrorChecking = false;
$sdoc->recover=true;
@$sdoc->loadHTMLFile($url);
$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);
$programme_arr = array();
$time_arr = array();
for ($i = 1; $i < 70; $i++)
{
$time_arr[] = $xpath->query("*/span[@id='time".$i."']");
$programme_arr[] = $xpath->query("*/span[@id='title".$i."']");
}
foreach($programme_arr as $programme)
{
$programme1 = $programme->item(0)->nodeValue;
}
$programme_title = $programme1;
// Generating to make the XML File
$xml .= "
<channel id='" . $my_id. " " . $channel . "'>";
$xml .= "
<display-name>" . $my_id. " " . $channel;
$xml .= " </display-name>";
// Save the output format
$DATE_FORMAT_STRING = "YmdHis";
// GET the current STAGE
$current_state = getState($array[0]->nodeValue);
$offset = 0;
$flag = 0;
foreach($time_arr as $time)
{
// Get the item state.
$this_state = getState($time->item(0)->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);
$starttime = $values[$count];
if($flag>0)
{
$xml .= '
<programme channel="'.$my_id. ' ' .$channel.'" start="'.$stoptime.'"
stop="'.$starttime.'">';
$xml .= '<title lang="en">'.$programme_title;
$xml .= ' </title>';
$xml .= '
<sub-title lang="en">';
$xml .= ' </sub-title>';
$xml .= '
<desc lang="en">';
$xml .= ' </desc>';
$xml .= '
<category lang="en">';
$xml .= ' </category>';
$xml .= '
</programme>';
$stoptime = $starttime;
}
else
{
$stoptime = $starttime;
$flag=1;
}
$current_state = $this_state;
$count++;
}
$xml .= '
</channel>';
}
}
}
$xml .= '
</tv>';
// output the xml to the browser in this example, write $xml to a file
here...
header("Content-Type: text/xml");
echo $xml;
$handle = fopen("myChannel.xml", "w");
fwrite ($handle, $xml);
?>
问题出现在这段代码中:
foreach($programme_arr as $programme)
{
$programme1 = $programme->item(0)->nodeValue;
}
$programme_title = $programme1;
xml .= '<title lang="en">'.$programme_title;
我尝试使用$xml .= '<title lang="en">'.$programme_title[$count];
,$programme_title = $programme1[$count];
,但不会输出每个不同的字符串。
你能不能告诉我如何在没有相同的字符串的情况下为输入获取每个不同的字符串?
答案 0 :(得分:1)
您可以通过此示例执行此操作:
<?php
$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input);
print_r($result);
?>