Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
function detect($body){
if(preg_match("%(?:youtube(?:-nocookie)?\.com/(?:[\w\-?&!#=,;]+/[\w\-?&!#=/,;]+/|(?:v|e(?:mbed)?)/|[\w\-?&!#=,;]*[?&]v=)|youtu\.be/)([\w-]{11})(?:[^\w-]|\Z)%i", $body)) {
preg_match("%(?:youtube(?:-nocookie)?\.com/(?:[\w\-?&!#=,;]+/[\w\-?&!#=/,;]+/|(?:v|e(?:mbed)?)/|[\w\-?&!#=,;]*[?&]v=)|youtu\.be/)([\w-]{11})(?:[^\w-]|\Z)%i", $body, $matches);
if(isset($matches[1])) {
$body = 'http://www.youtube.com/embed/'.$matches[1];
}else{
echo "";
}
}
else if(preg_match("%(?:youtube(?:-nocookie)?\.com/(?:[\w\-?&!#=,;]+/[\w\-?&!#=/,;]+/|(?:v|e(?:mbed)?)/|[\w\-?&!#=,;]*[?&]v=)|youtu\.be/)([\w-]{11})(?:[^\w-]|\Z)%i", $body)) {
preg_match("%(?:youtube(?:-nocookie)?\.com/(?:[\w\-?&!#=,;]+/[\w\-?&!#=/,;]+/|(?:v|e(?:mbed)?)/|[\w\-?&!#=,;]*[?&]v=)|youtu\.be/)([\w-]{11})(?:[^\w-]|\Z)%i", $body, $matches);
if(isset($matches[1])) {
$body = 'http://www.youtube.com/embed/'.$matches[1];
}else{
echo "";
}
}
// Google video
else if(preg_match("/video.google.com(.+)docid=([^&]+)/", $body)) {
preg_match("/docid=([^&]+)/", $body, $matches);
if(isset($matches[1])) {
$body = 'http://video.google.com/googleplayer.swf?docId='.$matches[1].'&hl=en';
}else{
echo "";
}
}
// Vimeo video
else if(preg_match("/vimeo.com\/[1-9.-_]+/", $body)) {
preg_match("/vimeo.com\/([1-9.-_]+)/", $body, $matches);
if(isset($matches[1])) {
$body = 'http://player.vimeo.com/video/'.$matches[1];
}else{
echo "";
}
}
// dailymotion video
elseif(preg_match('!^.+dailymotion\.com/(video|hub)/([^_]+)[^#]*(#video=([^_&]+))?|(dai\.ly/([^_]+))!', $body)) {
preg_match('!^.+dailymotion\.com/(video|hub)/([^_]+)[^#]*(#video=([^_&]+))?|(dai\.ly/([^_]+))!', $body, $matches);
if (isset($matches[6])) {
$body = 'http://www.dailymotion.com/embed/video/'.$matches[6];
}
if (isset($matches[4])) {
$body = 'http://www.dailymotion.com/embed/video/'.$matches[4];
}
else{
$body = 'http://www.dailymotion.com/embed/video/'.$matches[2];
}
}
return $body;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.