无法正确读取txt文件到PHP数组

发布于 2024-11-27 14:20:43 字数 2507 浏览 1 评论 0原文

我有一个在页面上显示 YouTube 视频的工作脚本,但我需要它从单独的(txt)文件中读取数组中的地址。我尝试了几种解决方案,但尚未找到一种有效的解决方案。 聪明人的任何帮助!

我的工作代码使用这样的数组:

$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 文件的第一行。至少我只能显示页面中的第一个视频。我的做法显然是错误的。 别说,我现在的代码一团糟。只要它仍然按计划工作,请随意修改和改进它。

I have working script that shows YouTube videos on page but I need it to read addresses in array from separate(txt) file. I tried several solutions but have not yet found a single working one.
Any help from smarter people!

My working code is using array like this:

$video = array('JObJ7vuppGE', 'BsmE6leTbkc', 'zWtj4TEVLvU');// array of youtube videos

What I planning to use instead:

$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>'; 
    }
}

unfortunately I am able to read only first line of txt file. At least I can only show the first video in page. My approach is obviously wrong.
And don`t tell, I now my code is mess. Feel free to modify and improve it, as long as it still works as planned.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

剧终人散尽 2024-12-04 14:20:43

我刚刚将您的代码复制到我本地安装的 XAMPP 中。

如果查询字符串中没有“v”参数,则会显示视频列表,每个视频都显示缩略图和标题,并在重定向回同一页面的链接内,但包含“v”参数。

单击缩略图会加载显示所选视频的 YouTube 播放器的页面。

我看这里没有问题...

I just copied your code across to my local install of XAMPP.

Without a 'v' parameter in the Query String, a list of videos were shown, each displaying a thumbnail and title and within a link which redirected back to the same page, but with a 'v' parameter included.

Clicking the thumbnail loaded the page showing a YouTube player for the selected video.

I see no problems here...

夢归不見 2024-12-04 14:20:43

我从你的问题中了解到你想从多个文件中读取视频 ID 并将其合并到一个 php 数组中?我说得对吗?

另外我建议使用 Zend_gdata api 来实现 YouTube 功能,它很简单,还有许多其他选项您可以尝试

what i understand from your question you want to read video id from multiple files and combine it into one php array? AM i correct?

Also i suggest using Zend_gdata api for youtube functionality its simple with many other options you can try

中性美 2024-12-04 14:20:43

我认为主要的想法/问题是将 ids 存储在 txt 文件中并读取它们,而不是将它们存储在数组中,对吧?

<?php

/**
 * @author - Sephedo
 * @for - dogcat @ Stackoverflow
 * @question - http://stackoverflow.com/questions/6936958/cant-read-txt-file-correctly-to-php-array
 */

$filename = "youtube.txt";

// Open the file
if( $handle = @fopen( $filename, "r") )
{
    // Cycle each line until end
    while(! feof( $handle ) )
    {
        $line = fgets($handle); // Read the line

        $youtube[] = $line;
    }
}

if(! isset( $youtube ) ) $youtube = array();

foreach( $youtube as $videoId )
{
    echo $videoId; // Etc etc etc.....
}

?>

I think the main idea / question was to store the ids in a txt file and read them rather than having them stored in an array right?

<?php

/**
 * @author - Sephedo
 * @for - dogcat @ Stackoverflow
 * @question - http://stackoverflow.com/questions/6936958/cant-read-txt-file-correctly-to-php-array
 */

$filename = "youtube.txt";

// Open the file
if( $handle = @fopen( $filename, "r") )
{
    // Cycle each line until end
    while(! feof( $handle ) )
    {
        $line = fgets($handle); // Read the line

        $youtube[] = $line;
    }
}

if(! isset( $youtube ) ) $youtube = array();

foreach( $youtube as $videoId )
{
    echo $videoId; // Etc etc etc.....
}

?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文