Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php replace youtube embed url

$youtubeUrl = 'https://www.youtube.com/watch?v=1cQh1ccqu8M';
$youtubePattern = "/s*[a-zA-Z//:.]*youtu(be.com/watch?v=|.be/)([a-zA-Z0-9-_]+)([a-zA-Z0-9/*-\_?&;\%=.]*)/i";
function youtubeEmbedCallback($matches) {
    if (!isset($matches[2])) {
        return '';
    }
    $videoId = $matches[2];
    return '<iframe width="560" height="315" src="https://www.youtube.com/embed/' . $videoId . '" frameborder="0" allowfullscreen></iframe>';
}
$youtubeEmbed = preg_replace_callback($youtubePattern, "youtubeEmbedCallback", $youtubeUrl);
echo $youtubeEmbed;
//Stamperà:
//<iframe width="560" height="315" src="https://www.youtube.com/embed/1cQh1ccqu8M" frameborder="0" allowfullscreen></iframe>
Comment

php get embed code from youtube url

preg_replace("/s*[a-zA-Z//:.]*youtube.com/watch?v=([a-zA-Z0-9-_]+)([a-zA-Z0-9/*-\_?&;\%=.]*)/i","<iframe width="420" height="315" src="//www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe>",$post_details['description']);
Comment

how to convert youtube url to embed code in php

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 ;
}
Comment

PREVIOUS NEXT
Code Example
Php :: wordpress 404.php redirect to home 
Php :: how to remove keys in subarray php 
Php :: how to serve the port in php 
Php :: unique check with multiple columns laravel validation 
Php :: how to use php to print inside html 
Php :: how to change php variable value in javascript 
Php :: wp plugins action link 
Php :: laravel logout current user 
Php :: how to start composer in laravel project on localhost 
Php :: php convert 
Php :: increase php memory 
Php :: php print array nice format 
Php :: php faker long text 
Php :: laravel eloquent many to many query using whereHas 
Php :: php unlink 
Php :: format a number with leading zeros in php 
Php :: reCAPTCHA v3 PHP 
Php :: fetch method and class in codeigniter 
Php :: php comment 
Php :: thousand seperator php 
Php :: laravel array cast 
Php :: write php online 
Php :: wordpress single post template 
Php :: Catches the last error php 
Php :: woocommerce checkout manager confirm password 
Php :: php array order alphabetically 
Php :: check if value change laravel 
Php :: php logout 
Php :: macos how host laravel website on localhost and intranet wifi 
Php :: associate laravel 
ADD CONTENT
Topic
Content
Source link
Name
2+9 =