MrSeth
MrSeth

Reputation: 51

Generate Random Youtube URLs from Text File & Embed with PHP

My goal is to have a widget on a sidebar of my website that generates a random video on every refresh.

I have created a text file that has a list of (50) youtube URLS and I'm using PHP to pull a random line from that text folder.

However I keep getting a syntax error on the return line (line 2) of this and can't figure out why. What am I missing, and(or) is there a more effective way around this code?

<?  

$lines = file('code.txt');
      return "<iframe width="250151" height="315" src="$lines[array_rand($lines)]" frameborder="0" allowfullscreen></iframe>";

?>

Upvotes: 0

Views: 519

Answers (2)

Lix
Lix

Reputation: 47996

Ok well you'll want to make sure you use the correct syntax for embedding variables into a string.

return '<iframe width="250151" height="315" src="'.$lines[array_rand($lines)].'" frameborder="0" allowfullscreen></iframe>';

Note that I'm using . concatenation to combine the two sides of the string with the variable in the middle. You'll need to change your quotes to single quotes for the return command in order to keep your double quotes inside the HTML content.

Upvotes: 2

Expedito
Expedito

Reputation: 7795

You need to escape your quotes.

return "<iframe width=\"250151\" height=\"315\" src=".$lines[array_rand($lines)]." frameborder=\"0\" allowfullscreen></iframe>";

Upvotes: 2

Related Questions