Last active
May 18, 2016 07:38
-
-
Save kamilsj/9422f3e58c03c0e0fade to your computer and use it in GitHub Desktop.
PHP embeding Youtube and Vimeo clips from url
This file contains 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 | |
function url($string, $width = 550, $height = 309) | |
{ | |
if(isset($string)){ | |
if(preg_match_all('/(http|https)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,6}(\/\S*)+?/', $string, $matches)){ | |
foreach($matches as $value){ | |
foreach($value as $key) { | |
//youtube clip | |
if(filter_var($key, FILTER_VALIDATE_URL) !== false) { | |
$url = parse_url($key); | |
if (strpos($url['host'], 'youtube') !== false) { | |
parse_str(parse_url($key, PHP_URL_QUERY), $vars); | |
$string = str_replace($key, '<iframe width="' . $width . '" height="' . $height . '" src="https://www.youtube.com/embed/' . $vars['v'] . '" frameborder="0" allowfullscreen></iframe>', $string); | |
} //vimeo clip | |
elseif (strpos($url['host'], 'vimeo') !== false) { | |
$v = substr($url['path'], 1); | |
$string = str_replace($key, '<iframe src="https://player.vimeo.com/video/' . $v . '" width="' . $width . '" height="' . $height . '" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>', $string); | |
} else { | |
$string = str_replace($key, '<a href="'.$key.'">'.$key.'</a>', $key); | |
} | |
} | |
} | |
} | |
} | |
unset($v, $value, $key, $vars); | |
} | |
return $string; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Now the script works perfectly! Yeah