Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

From file to array php

Tags:

arrays

php

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!

like image 267
Karol Jankiewicz Avatar asked Jun 27 '26 22:06

Karol Jankiewicz


1 Answers

$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>
like image 143
PHPDave Avatar answered Jun 29 '26 11:06

PHPDave