Last active
April 26, 2022 11:45
-
-
Save cptiwari20/ab8031be2f951d740a79e6bce776a790 to your computer and use it in GitHub Desktop.
A laravel helper that will convert any video link from youtube and Vimeo into the embed iframe link. You can use it in HTML or anywhere.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function setVideoPlayer($storedLink) { | |
| $videoSrc = ''; | |
| $videoLink = $storedLink; | |
| if (Str::contains($storedLink, ['youtube', 'youtu.be'])) { | |
| $videoSrc = 'YT'; | |
| if(Str::containsAll($storedLink, ['youtube', 'embed'])){ | |
| $videoLink = $storedLink; | |
| }else if(Str::contains($storedLink, ['youtube', 'watch'])){ | |
| $mainVal = Str::after($storedLink, '?v='); | |
| $videoLink = 'https://www.youtube.com/embed/'.$mainVal; | |
| }else if(Str::contains($storedLink, ['youtu.be'])){ | |
| $mainVal = Str::after($storedLink, "https://youtu.be/"); | |
| $videoLink = 'https://www.youtube.com/embed/'.$mainVal; | |
| } | |
| }else if(Str::contains($storedLink, ['vimeo'])){ | |
| $videoSrc = 'vimeo'; | |
| $mainVal = Str::after($storedLink, "https://vimeo.com/"); | |
| $videoLink = 'https://player.vimeo.com/video/'.$mainVal; | |
| } | |
| return $videoLink; // this videoLink can be used directly inside an iframe in HTML code. | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment