URL to Embed Code PHP Script (youtube,vimeo)

This article is about a code to convert url to embed code of youtube and viemo videos. In programming we needs this code for creating plugin or custom code.This article will covers how to convert url into embed code and also get thumbnail of youtube and vimeo videos.
You can see live demonstration of url to embed code generator for vimeo and youtube.


Beside this you can also customise the embed code such you can set custom height and width, enable autoplay.and the end you can download source code of url to embed code generator.
url to embed URL to Embed Code PHP Script (youtube,vimeo)
The idea is common we need to fetch video id from url in both youtube and vimeo, and after that we creates a custom embed link for both the website.height and width can be customise.Autoplay option seems to works with youtube only.For vimeo you can experiment with code,and let me know.

How to convert url to embed code from youtube url

  1. Get the video id
    $step1=explode(‘v=’, $url);
    $step2 =explode(‘&’,$step1[1]);
    $vedio_id = $step2[0];[/php]
  2. Create embed url from vedio id
    The iframe src of youtube is http://www.youtube.com/embed/$vedio_id so the embed code would be

    For autoplay you use autoplay=1 parameter.
  3. get youtube vedio thumbnail
    you can get thumbnail of any youtube vedio with the url
    Large thumbnail : http://img.youtube.com/vi/$vedio_id/0.jpgSmall thumbnail : http://img.youtube.com/vi/$vedio_id/1.jpg
    and so on…

How to convert url to embed code from vimeo url

  1. Get the video id.
    $vedio_id = str_replace(‘http://vimeo.com/’,”,$url);
  2. Create embed url from vedio id.
    The embed url for vimeo is
    $embedurl = “http://player.vimeo.com/video/”.$vedio_id;
    and iframe code for vimeo would be
  3. Get vedio thumbnail of vimeo.
    this step is slightly diffrent then youtube in this case we will  read video files in the vimeo server and locate its thumbnail image.
    $hash = unserialize(file_get_contents(“http://vimeo.com/api/v2/video/$vedio_id.php”));if (!empty($hash) && is_array($hash)) {

    $thumbnail_str =’ URL to Embed Code PHP Script (youtube,vimeo)‘;

    $fullsize_str = ‘ URL to Embed Code PHP Script (youtube,vimeo)‘;



I needed this code when i was creating a wordpress theme for a client
it was a video blog and client need this functionality to its wp admin panel with autoplay control.
you have to add just youtube url and it will automatically generate an embeded video on the front page.there is an option for embed code . you can see demo and download the source code below for free.Hope this thing helps you.Happy coding.



thanks for url to embed code php function for vimeo thumbnail.

Incoming terms:

  • Tellervo Warelius

    ve been looking everywhere for this! Thank goodness I found it on Bing. You have made my day! Thx again

  • ple

    what your download link

    • Rohit

      Hey Ple
      You have to like or tweet or +1 this post
      to download source code

  • Marcos

    Hi, I enjoyed your information tut. I did facebook like but had no dowload link appear. Please help. Thank you

  • Daniel

    Hello, Same here, I googled liked and didnt see a source code. Good Work mate!



    if ( strpos( $url, ‘youtube.com’ ) !== false ) {

    Youtube has more than one url for vídeos. So, to extract the video ID, it is more reliable to use regex like below:

    preg_match(“/^(?:http(?:s)?://)?(?:www.)?(?:youtu.be/|youtube.com/(?:(?:watch)??(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)/))([^?&"'>]+)/”, $url, $matches);

    Am i right?

    PS: sorry my bad english.

    • buffernow

      Thanks for the code :)