Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

how to convert youtube url to embed code in php

function getYoutubeEmbedUrl($url)
{
     $shortUrlRegex = '/youtu.be/([a-zA-Z0-9_-]+)??/i';
     $longUrlRegex = '/youtube.com/((?:embed)|(?:watch))((?:?v=)|(?:/))([a-zA-Z0-9_-]+)/i';

    if (preg_match($longUrlRegex, $url, $matches)) {
        $youtube_id = $matches[count($matches) - 1];
    }

    if (preg_match($shortUrlRegex, $url, $matches)) {
        $youtube_id = $matches[count($matches) - 1];
    }
    return 'https://www.youtube.com/embed/' . $youtube_id ;
}
Source by # #
 
PREVIOUS NEXT
Tagged: #convert #youtube #url #embed #code #php
ADD COMMENT
Topic
Name
4+4 =