Answers for "how to convert youtube url to embed code in php"

PHP
0

php convert link to embed youtube

function convertYoutube($string) {
    return preg_replace(
        "/s*[a-zA-Z//:.]*youtu(be.com/watch?v=|.be/)([a-zA-Z0-9-_]+)([a-zA-Z0-9/*-_?&;%=.]*)/i",
        "<iframe src="//www.youtube.com/embed/$2" allowfullscreen></iframe>",
        $string
    );
}
Posted by: Guest on June-07-2020
0

php replace youtube embed url

$youtubeUrl = 'https://www.youtube.com/watch?v=1cQh1ccqu8M';
$youtubePattern = "/s*[a-zA-Z//:.]*youtu(be.com/watch?v=|.be/)([a-zA-Z0-9-_]+)([a-zA-Z0-9/*-_?&;%=.]*)/i";
function youtubeEmbedCallback($matches) {
    if (!isset($matches[2])) {
        return '';
    }
    $videoId = $matches[2];
    return '<iframe width="560" height="315" src="https://www.youtube.com/embed/' . $videoId . '" frameborder="0" allowfullscreen></iframe>';
}
$youtubeEmbed = preg_replace_callback($youtubePattern, "youtubeEmbedCallback", $youtubeUrl);
echo $youtubeEmbed;
//Stamperà:
//<iframe width="560" height="315" src="https://www.youtube.com/embed/1cQh1ccqu8M" frameborder="0" allowfullscreen></iframe>
Posted by: Guest on May-07-2021
0

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 ;
}
Posted by: Guest on January-28-2022

Code answers related to "how to convert youtube url to embed code in php"

Browse Popular Code Answers by Language