[Drupal] How to check if URL entered is vimeo or youtube?

| | 1 min read

To add a video file inside the drupal site, there are different ways. If we are entering a video file inside the drupal site by entering the url inside a text field, and we need to find the url entered is of which type (ie the url is of youtube or vimeo type).

Take the url you entered and end explode with '/' to get the needed url part.

Example :

$id = end(explode('/', "the full url"));

For getting which content type, we need two types of explode

one for url with three values

Example:

www.example.com
$arr_url = explode('.', $k);
if (sizeof($arr_url) == 3) {
  $video_type = $arr_url[1];
}

Taking the first content to get the type of video.

Other for url with two values

Example :

example.com
  $arr_url1 = explode('//', $arr_url[0] );
  $video_type = $arr_url1[1];

After exploding the content type of the video

Check whether the video type is compare the video type we exploded and display in a frame for each type

Example :

For the vimeo content type

if($video_type == 'vimeo') {
  $url = 'http://player.example.com/video/'.$id;
  $output = '<iframe width="425" height="213" frameborder="0" allowfullscreen="" mozallowfullscreen="" webkitallowfullscreen=""  src="'.$url.'" id="video_frame"></iframe>';
}

Example :

For the youtube content type

else if ($video_type == 'youtube' )
{
  $id_val = explode("=", $id);
  $output = '<iframe width="420" height="213" frameborder="0" allowfullscreen="" mozallowfullscreen="" webkitallowfullscreen="" src="http://www.examle.com/embed/'.$id_val[1].'"></iframe>';
}