我正在為網站創建一些 EPG。我沒有經驗。不幸的是,我覺得很難。我怎樣才能得到圖示,這段代碼有什么問題?
Final code working:
<?php
$url = 'XML URL LINK';
$xml=simplexml_load_file("$url");
$channels = array();
foreach ($xml->channel as $c) {
$channels[ $c['id']->__toString() ] = $c->{'display-name'}->__toString();
}
$time = date( "YmdHi" );
foreach($xml->programme as $item) {
# or to search for programmes on a specific channel only:
# foreach($xml->xpath('programme[@channel="YOUR CHANNEL"]') as $item) {
$start = substr( (string)$item["start"], 0, -8);
$end = substr( (string)$item["stop"], 0, -8);
$link = (count($item->xpath('./icon/@src'))>0) ? ($item->xpath('./icon/@src'))[0] : ("No icon");
if ($time > $start && $time < $end) {
echo "Channel : ". $channels[ $item["channel"]->__toString() ]. "<br>";
echo date("G:i", strtotime($start)).' - '.date("G:i", strtotime($end)) . '<br>';
echo "Logo: <img src='{$link}'> <br/>" ;
echo "Title : ".$item->title. "<br>";
echo "Description : ".$item->desc. "<br>";
echo "<br>";
}
}
?>
uj5u.com熱心網友回復:
如果我對您的理解正確,那么這樣的事情應該會讓您接近我認為您正在嘗試做的事情。請注意,并非所有程式都有關聯的徽標。
$progs = $xml->xpath('//programme');
foreach ($progs as $prog) {
$title = $prog->xpath('./title/text()')[0];
$link = (count($prog->xpath('./icon/@src'))>0) ? ($prog->xpath('./icon/@src'))[0] : ("No icon");
echo($title .": ". $link);
echo "\r\n";
}
編輯:
我仍然不完全確定我了解您想要什么,但聽起來您需要將整個內容包裝在 html 中并輸出到瀏覽器。就像是:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
$url = 'https://raw.githubusercontent.com/HelmerLuzo/TDTChannels_EPG/master/TDTChannels_EPG.xml';
$xml=simplexml_load_file("$url");
$progs = $xml->xpath('//programme');
foreach ($progs as $prog) {
$title = $prog->xpath('./title/text()')[0];
$link = (count($prog->xpath('./icon/@src'))>0) ? ($prog->xpath('./icon/@src'))[0] : ("No icon");
echo "Logo: <img src='{$link}'> <br/>" ;
echo "Title : ".$title. "<br>";
echo "<br>";
}
?>
</body>
</html>
uj5u.com熱心網友回復:
這是一個有趣的。json_decode這使用已知的 hack 使用and將 xml 轉換為陣列json_encode,在我看來,這使得使用起來更容易。它還廣泛使用了DateTime物件。
$url = 'https://raw.githubusercontent.com/HelmerLuzo/TDTChannels_EPG/master/TDTChannels_EPG.xml';
$xml = simplexml_load_file($url);
$xml = json_decode(json_encode($xml), true); // Hack so we don't have to cast values in the future
// https://www.php.net/manual/en/timezones.php
$originalTimezone = 'Europe/Madrid';
$changeTimezone = 'Australia/Sydney'; // If empty, will not change timezone
//$outputFormat = 'Y-m-d H:i:s O';
$outputFormat = 'G:i';
$channels = [];
foreach ($xml['channel'] as $channel) {
$id = $channel['@attributes']['id'];
$channels[$id]['Display Name'] = $channel['display-name'];
$channels[$id]['Now Showing'] = [];
}
$now = new DateTime('now', new DateTimeZone($originalTimezone));
$programmes = [];
foreach ($xml['programme'] as $programme) {
$id = $programme['@attributes']['channel'];
$start = $programme['@attributes']['start'];
$start = DateTime::createFromFormat('YmdHis O', $start);
if (!empty($changeTimezone))
$start = $start->setTimezone(new DateTimeZone($changeTimezone));
$stop = $programme['@attributes']['stop'];
$stop = DateTime::createFromFormat('YmdHis O', $stop);
if (!empty($changeTimezone))
$stop = $stop->setTimezone(new DateTimeZone($changeTimezone));
if ($start < $now && $now < $stop) {
$nowShowing = [
'Title' => $programme['title'],
'Subtitle' => '',
'Description' => '',
'Date' => '',
'Start' => '',
'Stop' => '',
'Icon' => '',
];
if (isset($programme['sub-title']))
$nowShowing['Subtitle'] = $programme['sub-title'];
if (isset($programme['desc']))
$nowShowing['Description'] = $programme['desc'];
if (isset($programme['date']))
$nowShowing['Date'] = $programme['date'];
if (isset($programme['date']))
$nowShowing['Icon'] = $programme['icon']['@attributes']['src'];
$nowShowing['Start'] = $start->format($outputFormat);
$nowShowing['Stop'] = $stop->format($outputFormat);
$channels[$id]['Now Showing'] = $nowShowing;
}
}
foreach ($channels as $id => $details) {
echo $details['Now Showing']['Start'] . ' - ' . $details['Now Showing']['Stop'] . '<br>' .
'Channel:' . $details['Display Name'] . '<br>' .
'Title: ' . $details['Now Showing']['Title'] . '<br>' .
'Info: ' . $details['Now Showing']['Subtitle'] . '<br>' .
'Description: ' . $details['Now Showing']['Description'] . '<br>' .
'Logo: <img src="'.$details['Now Showing']['Icon'].'"/><br><br>';
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/474958.html
上一篇:來自Google測驗的Xml生成不顯示跳過的測驗用例
下一篇:如何添加標簽?
