Jump to content
New account registrations are disabed. This website is now an archive. Read more here.
Sign in to follow this  
Marked

Showing Youtube vidoes on the page from a URL

Recommended Posts

The following is a simple tutorial to show how you can display a Youtube video on your website from using the URL. This is useful for when you have a form for your users of whatever and you want them to submit a youtube video, and the simplest way for them is to copy and paste the URL.

 

This is a similar code that I am using in something im making, it currently looks something like this:

Example.JPG

 

Alright, firstly we are going to take the code that youtube gives us for posting youtube videos on other websites. Go into any youtube video and click the 'embed' button under the video and you will something similar to this:

<iframe title="YouTube video player" class="youtube-player" type="text/html"
width="480" height="390" 
src="http://www.youtube.com/embed/8VtUYvwktFQ" frameborder="0" allowFullScreen></iframe>

 

This is the code we'll use to display the video. In it is the ID of the video, you'll see it in the src, in the code it is: src="http://www.youtube.com/embed/8VtUYvwktFQ". What we want to do is take this ID from our youtube links and place it into this code. How we are going to do this is with regular expression.

 

The following code gets youtube ID's from URLs.

<?php
$regex='#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=[0-9]/)[^&\n]+|(?<=v=)[^&\n]+#';
preg_match($regex, $YOUTUBE_URL, $id);
?>

How this code works really isn't very important. Its complicated, and you should just be satsified that it works. The variable $YOUTUBE_URL is going to be your youtube url string, such as 'http://www.youtube.com/watch?v=8VtUYvwktFQ'. It may be a $_POST variable from your form or whatever. The ID is now stored the variable $id which an an array. Its array because it stores all matches. In this case it should only return one match, so our ID is accessible with this: $id[0]

 

Now we simple plug in this ID into our embed code. Just mix this your way with html and PHP. I generally open and close php tags instead of doing something like this: echo '

'.$var.'
', that just seems messy to me.

 

Ok here's the final code.

<?php

//the youtube url string
$youtube_url='http://www.youtube.com/watch?v=8VtUYvwktFQ';

//use regex to get the video ID
$regex='#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=[0-9]/)[^&\n]+|(?<=v=)[^&\n]+#';
preg_match($regex, $youtube_url, $id);

//plug that into our html
?>
<iframe title="YouTube video player" class="youtube-player" type="text/html"
width="480" height="390" 
src="http://www.youtube.com/embed/<?=$id[0]?>" frameborder="0" allowFullScreen></iframe>

 

Hope this helps someone. :)

 

Ok, here's the final code.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...