开发者

Can't read txt file correctly to PHP array

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 = $x开发者_Python百科mlData;

            $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.


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...


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


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.....
}

?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜