Skip to content

Instantly share code, notes, and snippets.

@kamilsj
Last active May 18, 2016 07:38
Show Gist options
  • Save kamilsj/9422f3e58c03c0e0fade to your computer and use it in GitHub Desktop.
Save kamilsj/9422f3e58c03c0e0fade to your computer and use it in GitHub Desktop.
PHP embeding Youtube and Vimeo clips from url
<?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;
}
@kamilsj
Copy link
Author

kamilsj commented Oct 22, 2015

Now the script works perfectly! Yeah

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment