Created
September 23, 2016 08:33
-
-
Save noomz/0d41014788f222f34553ffb58c66f7ae to your computer and use it in GitHub Desktop.
Show youtube embed as width 100%
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
<?php | |
$youtube_url = 'https://www.youtube.com/watch?v=UeR_jEx6Pzs'; | |
// or | |
$youtube_url = '<iframe width="560" height="315" src="https://www.youtube.com/embed/UeR_jEx6Pzs" frameborder="0" allowfullscreen></iframe>'; | |
if ($youtube_url) { | |
if (preg_match('/iframe/', $youtube_url)) { | |
$iframe_url = preg_replace('/width=[\"\'].*?[\"\']/', 'width="100%"', $youtube_url); | |
$iframe_url = preg_replace('/height=[\"\'].*?[\"\']/', 'height="100%"', $iframe_url); | |
$fixed = $iframe_url; | |
} | |
else { | |
$video_id = preg_replace("/.*?[\&\?]v=([a-zA-Z0-9_]+)?.*/", "$1", $youtube_url); | |
$fixed = '<iframe width="100%" height="100%" src="https://www.youtube.com/embed/' . $video_id . '" frameborder="0" allowfullscreen></iframe>'; | |
} | |
} | |
print $fixed; |
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
.youtube-wrapper { | |
position: relative; | |
width: 100%; | |
height: 0; | |
padding-bottom: 56.25%; | |
margin: 0 0 15px; | |
iframe { | |
position: absolute; | |
width: 100%; | |
height: 100%; | |
left: 0; | |
top: 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment