Iframe for youtube with the entire link

1

Hello

I would like to know if there is any iframe or something I can get by the video on my site, in the manageable case, usually youtube embeds are with the video ID. I wonder if there are any that I can paste the entire YouTube url that works on the page. Here is the example of what I use (remembering that I only copy the id of the video)

 <iframe width="100%" height="450" src="http://www.youtube.com/embed/<?phpprint$insti->video?>"></iframe>

You can see that after the / embed is inserted the id of the video that is registered, but I need some embed that I may be pasting the entire youtube link, example:

<iframe width="100%" height="450" src="https://www.youtube.com/watch?v=Z9VIEZhFORE"></iframe>

I'vebeenthinkingoftreatingtheURL,explodingandgettingthedataonlyafter=onlythatsometimestheshortURLofthevideocanberegistered,whichendsuplikethis: is not going to work out.

    
asked by anonymous 03.03.2016 / 18:01

1 answer

3

If this takes the URL: <?php print $insti->video ?> then you can make a parse of the ID, a good example in the SOen like this:

<?php
/**
 * get youtube video ID from URL
 *
 * @param string $url
 * @return string Youtube video id or FALSE if none found. 
 */
function youtube_id_from_url($url) {
    $pattern = 
        '%^# Match any youtube URL
        (?:https?://)?  # Optional scheme. Either http or https
        (?:www\.)?      # Optional www subdomain
        (?:             # Group host alternatives
          youtu\.be/    # Either youtu.be,
        | youtube\.com  # or youtube.com
          (?:           # Group path alternatives
            /embed/     # Either /embed/
          | /v/         # or /v/
          | /watch\?v=  # or /watch\?v=
          )             # End path alternatives.
        )               # End host alternatives.
        ([\w-]{10,12})  # Allow 10-12 for 11 char youtube id.
        $%x'
        ;
    $result = preg_match($pattern, $url, $matches);
    if ($result) {
        return $matches[1];
    }
    return false;
}
?>

<iframe width="100%" height="450" src="http://www.youtube.com/embed/<?phpechoyoutube_id_from_url($insti->video);?>"></iframe>

It supports any Youtube url, including embed and short.

    
03.03.2016 / 18:08