我有工作腳本顯示頁面上的YouTube視頻,但我需要它從單獨的(txt)文件讀取數組中的地址。我嘗試了幾個解決方案,但尚未找到一個工作。 聰明人的幫助!無法正確讀取txt文件到PHP陣列
我的工作代碼使用數組是這樣的:
$video = array('JObJ7vuppGE', 'BsmE6leTbkc', 'zWtj4TEVLvU');// array of youtube videos
我打算什麼改用:
$tube = $_SERVER['DOCUMENT_ROOT'] .'/youtube.txt';// IDs of youtube videos inside
$lines = file($tube);
foreach($lines as $line_num => $line)
$video = array($line);// array of youtube videos
$v = preg_replace("/[^A-Za-z0-9\-\_]/", "", $_GET["v"]); // make sure "v" parameter is sanitized
if(!empty($v) && in_array($v, $video)) //check if parameter "v" is empty and is yours video
{
echo '<object width="853" height="505"><param name="movie" value="http://www.youtube.com/v/' . $v . '&hl=en_US&fs=1&hd=1&color1=0xe1600f&color2=0xfebd01"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param>
<embed src="http://www.youtube.com/v/' . $v . '&hl=en_US&fs=1&hd=1&color1=0xe1600f&color2=0xfebd01" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="853" height="505"></embed>
</object>';
}
else
{
foreach($video as $v)
{
echo '<div class="tube">
<a href="/sites/tube.php?v=' . $v . '" class="thickbox">
<img style="border: 2px solid #000; margin: 5px;" alt="" src="http://i1.ytimg.com/vi/' . $v . '/default.jpg"/>
</a>';
$xmlData = simplexml_load_string(utf8_encode(file_get_contents('http://gdata.youtube.com/feeds/api/videos/' . $v . '?fields=title,yt:recorded')));
$title = (string)$xmlData->title;
$entry = $xmlData;
$namespaces = $entry->getNameSpaces(true);
$yr = $entry->children((string)$namespaces['yt']);
// get <yt:recorded> node for date and replace yyyy-mm-dd to dd.mm.yyyy
$year = substr($yr->recorded, 0, 4);
$month = substr($yr->recorded, 5, 2);
$day = substr($yr->recorded, 8, 2);
$recorddate = $day . "." . $month . "." . $year;
echo '<p>' . $recorddate . '<br>' . $title . '<br>
</p>
</div>';
}
}
不幸的是,我能讀txt文件的只有第一行。至少我只能在頁面上顯示第一個視頻。我的做法顯然是錯誤的。 而且不說,我現在的代碼很混亂。隨時修改並改進它,只要它仍然按計劃運行。
如果您不介意,請在問題標題和標記中添加腳本語言。 – sudmong
目前還不清楚你正在嘗試做什麼,或者目前代碼中目前失敗的內容。請閱讀http://tinyurl.com/so-hints –
請勿發佈您的工作代碼。將您嘗試使用的代碼從文本文件讀取到數組中。這就是你想要的幫助,所以這是我們不需要看的。 – gnud