DEV Community

Cover image for View embedded videos from youtube, vimeo and google drive
Walter Nascimento
Walter Nascimento

Posted on

2

View embedded videos from youtube, vimeo and google drive

I had to make a small adjustment that was necessary that whenever the user entered a new url, I would adjust it to the valid format of youtube and vimeo, and in that I thought it takes a little work, so to make the next one easier, I'll leave the code ready here.

CODE

FrontEnd

For the interface you can just use the html iframe

<iframe width="100%" height="460" 
src="{{url}}"
title="Video player" frameborder="0"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen></iframe>
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ NOTE: In the src property is where we apply our formatted URL

BackEnd

For the back end you just need a simple method.

public function formatUrl($url)
{
        $urlParse = parse_url($url);
        // Extract id from youtube url
        if (strpos($urlParse["host"], 'youtube') !== false) {
            if (isset($urlParse["query"])) {
                parse_str($urlParse["query"], $query);
                return "https://www.youtube.com/embed/" . $query["v"];
            }
            $id = explode("/", $urlParse["path"]);
            return "https://www.youtube.com/embed/" . end($id);
        }
        // Extract id from vimeo url
        if (strpos($urlParse["host"], 'vimeo') !== false) {
            $urlParse = explode("/", $urlParse["path"]);
            return "https://player.vimeo.com/video/" . $urlParse[1];
        }
        // Extract id from google drive url
        if (strpos($urlParse["host"], 'drive.google') !== false) {
            $urlParse = explode("/", $urlParse["path"]);
            $urlParse = array_values(array_filter($urlParse));
            return "https://drive.google.com/file/d/" . $urlParse[2] . "/preview";
        }
}
Enter fullscreen mode Exit fullscreen mode

That's it

Extra

If you want the full php class, follow it below.

<?php
namespace App\Controller;
class Video
{
private static $URLYoutube = "https://www.youtube.com/embed/";
private static $URLVimeo = "https://player.vimeo.com/video/";
private static $URLGoogleDrive = "https://drive.google.com/file/d/";
public static function view($url)
{
$formatUrl = self::formatUrl($url);
$allows = [
'accelerometer', 'autoplay', 'clipboard-write',
'encrypted-media', 'gyroscope', 'picture-in-picture'
];
$allows = implode("; ", $allows);
return "<iframe
width='100%'
height='460'
src='$formatUrl'
title='Video Player'
frameborder='0'
allow='$allows'
allowfullscreen>
</iframe>";
}
public static function formatUrl($url)
{
if (self::validarUrl($url)) {
$urlParse = parse_url($url);
// Extract id from youtube url
if (strpos($urlParse["host"], 'youtube') !== false) {
if (isset($urlParse["query"])) {
parse_str($urlParse["query"], $query);
return self::$URLYoutube . $query["v"];
}
$id = explode("/", $urlParse["path"]);
return self::$URLYoutube . end($id);
}
// Extract id from vimeo url
if (strpos($urlParse["host"], 'vimeo') !== false) {
$urlParse = explode("/", $urlParse["path"]);
return self::$URLVimeo . $urlParse[1];
}
// Extract id from google drive url
if (strpos($urlParse["host"], 'drive.google') !== false) {
$urlParse = explode("/", $urlParse["path"]);
$urlParse = array_values(array_filter($urlParse));
return self::$URLGoogleDrive . $urlParse[2] . "/preview";
}
}
}
public static function validarUrl($url)
{
$regex = '|^http(s)?://[a-z0-9-]+(\.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i';
return preg_match($regex, $url);
}
}
view raw checkurl.php hosted with ❀ by GitHub

Thanks for reading!

If you have any questions, complaints or tips, you can leave them here in the comments. I will be happy to answer!
😊😊 See you! 😊😊


Support Me

Youtube - WalterNascimentoBarroso
Github - WalterNascimentoBarroso
Codepen - WalterNascimentoBarroso

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs