This PHP function takes a YouTube embed link. E.g.:
https://www.youtube.com/embed/idGoesHere
And converts it into an iframe that embeds the YouTube video. This can be a great timesaver for your clients. All you do is pass your $content
(which should be HTML) and all parsed YouTube URLs will be converted in the returned string.
function iframeFromYouTube($content) {
$pattern = '/https?:\/\/(www\.)?youtube\.com\/watch\?v=([a-zA-Z0-9_-]+)/';
$callback = function($matches) {
$videoID = $matches[2];
$embedURL = "https://www.youtube.com/embed/" . $videoID;
return '<p><iframe data-cubed-video-ratio="16:9" style="width: 100%;" width="560" height="315" src="' . $embedURL . '" frameborder="0" allowfullscreen></iframe></p>';
};
$content = preg_replace_callback($pattern, $callback, $content);
return $content;
}
