What is the best way to embed Youtube videos? [clo

2019-02-25 21:53发布

问题:

I'm building a video website and I want my users to be able to upload or embed videos from Youtube. The Youtube API doesn't seem to provide video upload from their website, or do they? So I tried to embed videos via an iframe with PHP. Here is the code:

<?php
$source=$_POST['source'];
?>

<html>
<div id="bo"><?php echo $source;?></div>
<form action="altupload.php" method="post">
<input type="text" name="source">
<input type="submit">
</form>
</html>

But all I get is:

<html><div id="bo"><iframe width=\"560\" height=\"315\" src=\"http://www.youtube.com/embed/tZa5hyb0_wA\" frameborder=\"0\" allowfullscreen></iframe></div>`

<html>
<form action="altupload.php" method="post">
<input type="text" name="source">
<input type="submit">
</form>
</html>

Can anyone recommend some other ways to do it? Thanks

回答1:

Allow the user upload only youtube video ID from URL as :

http://www.youtube.com/watch?v=Vw5FDZRGTL0

video ID : Vw5FDZRGTL0

and you can set other options of IFrame tag :

<iframe width="420" height="315" src="http://www.youtube.com/embed/<?php echo $videoID; ?>" frameborder="0" allowfullscreen></iframe>  

Good Luck



回答2:

You need to use the addslashes() function:

echo Addslashes($source);