3

I want to display videos on my website. In file I have urls (separator | ) I can't do this: Do array from text in .txt. Get 4 random urls from array.

youtube_vids.txt

https://www.youtube.com/embed?v=oavMtUWDBTM|
https://www.youtube.com/embed?v=dQw4w9WgXcQ|
https://www.youtube.com/embed?v=djV11xsamQ|
https://www.youtube.com/embed?v=Tj75ArXbc914|
https://www.youtube.com/embed?v=9jK-NcRmVcw|
https://www.youtube.com/embed?v=n4RjJKhq5ho|
https://www.youtube.com/embed?v=6Ejga4kJUts|

I stopped here:

$vid_list = file('youtube_vids.txt');

Can anyone help me? Thanks a lot!

2
  • Is each url on a new line or does it just appear that way due to wordwrap? Commented Feb 7, 2016 at 3:11
  • each url is on a new line Commented Feb 7, 2016 at 3:12

4 Answers 4

1
$videoArray = file('youtube_vids.txt');
$randomIndexes = array_rand($videoArray, 4);
foreach ($randomIndexes as $index) {
    echo '<iframe width="420" height="315" src="'.preg_replace( "/\r|\n/", "", str_replace('|','',$videoArray[$index])).'"></iframe>';
}

will output

<iframe width="420" height="315" src="https://www.youtube.com/embed?v=dQw4w9WgXcQ"></iframe>
<iframe width="420" height="315" src="https://www.youtube.com/embed?v=oavMtUWDBTM"></iframe>
<iframe width="420" height="315" src="https://www.youtube.com/embed?v=6Ejga4kJUts"></iframe>
<iframe width="420" height="315" src="https://www.youtube.com/embed?v=dQw4w9WgXcQ"></iframe>
Sign up to request clarification or add additional context in comments.

Comments

1

The file() function separates the array by newline, so the | separator is unused and has to be trimmed:

$vid_list = file('youtube_vids.txt');
shuffle($vid_list);
for ($i = 0; $i < 4; $i++) {
    // Trim any newline characters
    $url = trim($vid_list[$i]);
    // Then trim the 'separator'
    $url = trim($url, '|');
    echo $url . "<br/>";
}

Comments

1

The solution is:

  • First read the entire file into an array using file()
  • Then select 4 random line numbers using array_rand()
  • And finally explode() and extract URLs from the array

So your code should be like this:

$lines = file('youtube_vids.txt');
$rand_array = array_rand($lines, 4);

$random_urls = array();
foreach ($rand_array as $line) {
    $random_urls[] = explode("|",$lines[$line])[0];
}

// display $random_urls array
echo "<pre>";
print_r($random_urls);
echo "</pre>";

Comments

1
$filename = "file.txt"; //Input Text File With Separators

if(file_exists($filename)) { //Check if file exists

    $myfile = fopen($filename, "r") or die("Unable to open file!"); //Reading file

    $fileData = fread($myfile,filesize($filename)); //Save file data in a variable

    $splitData = explode("|", $fileData); //Split file data using separators

    $randLinks = array_rand($splitData, 4); // Get 4 random indices from splitData array

   foreach($randLinks as $randLink) { // Foreach index
        echo $splitData[$randLink] . "<br>"; // Get url
    }

   fclose($myfile); //Don`t forget to close the file 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.