Cách dịch liên kết youtube thành iframe trong laravel với các ví dụ

Trong bài viết này mình sẽ minh họa 1 cách sử dụng Cách dịch link youtube thành iframe trong laravel với các ví dụ .

Cách dịch liên kết youtube thành iframe trong laravel với các ví dụ - dịch liên kết youtube thành iframe trong laravel
There are two types of youtube link for one video:

Example:

$link1 = 'https://www.youtube.com/watch?v=NVcpJZJ60Ao';
$link2 = 'https://www.youtu.be/NVcpJZJ60Ao';
This function handles both:

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 ;
}
The output of $link1 or $link2 would be the same :

 $output1 = getYoutubeEmbedUrl($link1);
 $output2 = getYoutubeEmbedUrl($link2);
 // output for both:  https://www.youtube.com/embed/NVcpJZJ60Ao
Now you can use the output in iframe!

Nếu bạn không hài lòng với câu trả lời của tôi về Cách dịch liên kết youtube thành iframe trong laravel với các ví dụ. Xem thêm các chủ đề tương tự hoặc để lại cho tôi một câu hỏi mới

chưa xác định