Search
 
SCRIPT & CODE EXAMPLE
 

PHP

translate youtube link into iframe in 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!
Comment

PREVIOUS NEXT
Code Example
Php :: laravel clear table 
Php :: fixuphost 
Php :: php array formatted output 
Php :: remove double space php 
Php :: add post meta wordpress 
Php :: delete file laravel 8 
Php :: cmd run powershell command 
Php :: woocommerce change sale text 
Php :: php form examples tutorials with code 
Php :: current loggedin user laravel 
Php :: php pdo Check if row exists in the database 
Php :: wordpress throw to debug.log 
Php :: laravel $loop interation 
Php :: convert json object to array in php 
Php :: how to add new column in laravel migration 
Php :: laravel generate unique token 
Php :: php get all saturdays in a month 
Php :: get number of chars ina string php 
Php :: laravel migration add date of birth column 
Php :: laravel php artisan make:controller in subfolder 
Php :: Laravel Validation check array size min and max 
Php :: drupal 8 date formater service 
Php :: newline in php 
Php :: remove space from start and end of string in php 
Php :: get last month php 
Php :: remove first element in array php 
Php :: sha256 in php 
Php :: slp price php 
Php :: is home page if wordpress 
Php :: rearrange array index php 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =