I have a form which a user can enter a youtube link along with their product. I want to be able to play that video (which it's link is stored in a MySQL database, the problem is that the <iframe> tag needs a youtube "Embed" link and not the URL, is there a way to convert the URL to the Embed link?
video_url = https://www.youtube.com/watch?v=1DIDWWKk8Bg
//code to connect to MySQL DB and get all rows
$videourl = $row["video_url"];
}
?>
<iframe width="420" height="345" src="<?php $videourl?>">
</iframe>
Regular YouTube link look like this
https://www.youtube.com/watch?v=1234
Embed YouTube link look like this
https://www.youtube.com/embed/1234
You need just to replace this watch?v= to embed/
$link = "https://www.youtube.com/watch?v=1234";
echo str_replace("watch?v=", "embed/",$link);
You have to use embed.
like this.
$videourl = explode("https://www.youtube.com/watch?v=", $row['videourl']);
<iframe class="iframeVideo" src="https://www.youtube.com/embed/' . $videourl[1] . '"></iframe>
$videourl has to look like this https://www.youtube.com/watch?v=3J6o7hcm8bE
I hope this works for you.
You cannot not use the exact url from the youtube video. The url used in iframes has the following format http://www.youtube.com/embed/unique_id_here. So you only have to save the unique_id for each video. It is only a part of the url. For example, from this url: https://www.youtube.com/watch?v=hdhjs88737&list=RDEMnLv8DM0l6F9uGd8jrENGgg&index=3, the unique id is the one in bold: hdhjs88737. So here is what I would do. Go to any video on youtube and click the share option, under the options select embed. Then copy the code. It will look like this:
<iframe width="560" height="315" src="https://www.youtube.com/embed/<unique_id_here>" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
Modify the parameters in the iframe to fit your preferences then only save the unique_id to your database. That should solve your problem. Check this API reference for a detailed guide.
Here is what I used, I'm not sure if this code should be there twice but it works so...
<?php
str_replace("watch?v=", "embed/",$videourl);
?>
<iframe width="520" height="345" src="<?php echo str_replace("watch?v=", "embed/",$videourl);?>">
</iframe>
Thank you for the help.
Related
I'm trying to develop in my website a preview of youtube link like facebook.
For example: If i set in comment a link of youtube video like https://www.youtube.com/watch?v=7TF00hJI78Y, a preview of this video will be showed in comment.
How can i do this??
Thanks.
You have to replace the link or url with an Iframe
For you
https://www.youtube.com/watch?v=7TF00hJI78Y
you have to replace that text for -->
<iframe width="560" height="315" src="https://www.youtube.com/embed/7TF00hJI78Y" frameborder="0" allowfullscreen></iframe>
https://www.youtube.com/embed/7TF00hJI78Y?start=500&end=600
start en end video and with javascript you can turn off the music.
and then onclick you can remove the start en end from the embed and put the music on method doesn't work anymore.
I want to play youtube video link on my php page
youtube link is
https://www.youtube.com/watch?v=VAJK04HOLd0
<object width="420" height="315" data="https://www.youtube.com/watch?v=VAJK04HOLd0">
i am try this code but it's not working
You can load the video file in <iframe>. Try this sample code,
<iframe width="560" height="315" src="https://www.youtube.com/embed/VAJK04HOLd0" frameborder="0" allowfullscreen></iframe>
Look at the last part of the url https://www.youtube.com/embed/VAJK04HOLd0,
VAJK04HOLd0
Which is the youtube video code. If you want to change the video change the code.
To embed YouTube videos, you should be using an iframe.
<iframe id="ytplayer" type="text/html" width="640" height="390" src="http://example.com/?autoplay=1" frameborder="0"/>
It's not connected to php.
You will want to include &autoplay=1 to the youtube string.
I want to display a youtube embed using the URL that the user put on their profile.
It works correctly if the URL in the database is http://youtube.com/embed/URL
I'd like users to be able to just input a regular URL (http://youtube.com/URL because I want it to be easy for them.
Is there a way to get around this?
Thanks a lot for any help, I'm quite new at this!
Try copying and pasting this, and see if it works:
echo '<br>';
echo '<iframe width="560" height="315" src="' . stripslashes($row['videourl']) . '" frameborder="0" allowfullscreen></iframe>';
If it's not showing up based on the fact that the person entering the URL doesn't place embed in their link, then what I would recommend is just having the person enter the ID of the video, and you create the rest of the URL.
For example, if the URL to the video a user wants to post is https://www.youtube.com/watch?v=0TL5CSPFzTU then they would just enter 0TL5CSPFzTU and you would output your code as such:
echo '<iframe width="560" height="315" src="https://www.youtube.com/embed/' . stripslashes($row['videourl']) . '" frameborder="0" allowfullscreen></iframe>';
You'd have to put something in place that stops them from entering the full URL, or anything more than the ID, but there are plenty of tutorials out there for accomplishing that.
I'm trying to keep a database of posted videos on a site I am currently developing. To be able to do this I have to store the URLs of the videos into a database and then I call the url from the database using php and sql by doing this bare in mind I am using this from a php defined area on my homepage. If I use this code it works on other video post sites!
echo '<iframe width="640" height="390" src="', $row['Post_URL'],'"frameborder="0" allowFullScreen></iframe>';
This will not work it says content cannot be displayed in a frame but if I do this it works.
<?php
echo '<iframe width="560" height="315" src="http://www.youtube.com/embed/wardQ7W8hPU" frameborder="0" allowfullscreen></iframe>';
?>
why does the one work properly and the other not? what is a way around this so I can call from the database into php and echo it out so it will display properly on the page?
I got the same problem today. As stated in one of the comments, the problem is the url used...
The iframe embed only works with urls looking like:
http://www.youtube.com/embed/[video_id]
Most of people are using the "share" feature, that generates minified urls like:
http://youtu.be/[video_id]
However, if you look into the "embed" feature, you'll notice that it generates an iframe with src urls like the first example.
So to answer the question, when using iframes to embed, use the src url like the first example.
Try this
<iframe width="640" height="390" src="<?php echo $row['Post_URL'] ?>" frameborder="0" allowFullScreen></iframe>
OR this
<?php
echo "<iframe width='640' height='390' src='{$row['Post_URL']}' frameborder='0' allowFullScreen></iframe>";
?>
OR this
<?php
echo "<iframe width='640' height='390' src='" . $row['Post_URL'] . "' frameborder='0' allowFullScreen></iframe>";
?>
they all are the same and will do the same process.
I'm doing this with dynamic data and it's easier for the client to use the youtube Share link.
So... it's then just a matter of getting the youtube video ID from that url.
// we can't use the "share" link so need to just get the video ID
$youtubePath = parse_url($youtubeShareLink,PHP_URL_PATH);
$youtubeId = str_replace('/','',$youtubePath);
and now we can use the ID in an iframe (adjust width and height per embed options in youtube)
<iframe width="560" height="315" src="//www.youtube.com/embed/<? echo $youtubeId; ?>?rel=0" frameborder="0" allowfullscreen></iframe>
I have a form for updating my database. The form contains an embed-video-section which calls the embed code on a separate page to display the video in the page.
The problem is that embed links uses <iframes> which would be removed by my striptags.
I need some help in safely inserting the embed code from my form to my database.
The embed codes look like this
<iframe width="420" height="315" src="http://www.youtube.com/embed/Ahg6qcgoay4" frameborder="0" allowfullscreen></iframe>
or <iframe src="http://blip.tv/play/hIVVgfKwRgI.html" width="550" height="339" frameborder="0" allowfullscreen></iframe>
You could try to use BBCodes. For example, [embed param1=value param2=value] and so on..