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>
π‘ 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";
}
}
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); | |
} | |
} |
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
Top comments (0)