Embed Video Player

Video Class code attached. You can put this file in any directory. Previously it was put in a folder called classes.

Calling video class

$link = "https://www.youtube.com/watch?v=NpEaa2P7qZI";
$embed = Video::get_embed_URL($link)."?&autoplay=1&loop=1";

Video Class

class Video {
const _youtube_regex = '/(\/\/.*?youtube\.[a-z]+)\/watch\?v=([^&]+)&?(.*)/';
const _youtubeshort_regex = '/(\/\/.*?youtu\.be)\/([^\?]+)(.*)/i';
const _vimeo_regex = '/(\/\/.*?)vimeo\.[a-z]+\/([0-9]+).*?/';

/**
 * Given filetype column from DB's article_medias table will return true if the media is video
 * @param string $source source from article_medias.filetype column
 * @return bool True if video
 */
public static function is_video( $source ) {
	if ($source == 'youtube' || $source == 'youtu.be' || $source == 'vimeo') {
		return true;
	} else {
		return false;
	}
}


/**
 * Given a url param to a youtube or vimeo video, will return the related format
 * @param string $url Url to the video
 * @return bool|string
 */
public static function get_source( $url ) {
	if (preg_match(self::_youtube_regex, $url)) {
		$format = 'youtube';
	} else if (preg_match(self::_youtubeshort_regex, $url)) {
		$format = 'youtu.be';
	} else if (preg_match(self::_vimeo_regex, $url)) {
		$format = 'vimeo';
	} else {
		return false;
	}

	return $format;
}



/**
 * Given a video url to either a youtube or vimeo video, will return the specific video id
 * @param string $url Video url
 * @return bool|string
 */
public static function get_source_ID( $url ) {
	$format = self::get_source($url);

	switch ($format) {
		case 'vimeo':
			$vid_id = preg_replace(self::_vimeo_regex, '$2', $url);
			if (substr($vid_id, 0, 5) == "http:") { $vid_id = substr($vid_id, 5); }
			if (substr($vid_id, 0, 6) == "https:") { $vid_id = substr($vid_id, 6); }
			break;

		case 'youtube':
			$vid_id = rtrim(preg_replace(self::_youtube_regex, '$2', $url), '?');
			if (substr($vid_id, 0, 5) == "http:") { $vid_id = substr($vid_id, 5); }
			if (substr($vid_id, 0, 6) == "https:") { $vid_id = substr($vid_id, 6); }
			break;

		case 'youtu.be':
			$vid_id = rtrim(preg_replace(self::_youtubeshort_regex, '$2', $url), '?');
			if (substr($vid_id, 0, 5) == "http:") { $vid_id = substr($vid_id, 5); }
			if (substr($vid_id, 0, 6) == "https:") { $vid_id = substr($vid_id, 6); }
			break;

		default:
			return false;
	}
	return $vid_id;
}


 /**
	* Gets the html url for the video
	* @param string $id Video id
	* @param string $format Video format/source
	* @return bool|string returns url for embedding the video
	*/
public static function get_embed_URL($url) {
	$format = self::get_source($url);
	$id = self::get_source_ID($url);

 switch ($format) {
	 case 'vimeo':
		 $link = 'https://player.vimeo.com/video/'.$id;
		 break;

	 case 'youtube':
		 $link = 'https://www.youtube.com/embed/'.$id;
		 break;

	 case 'youtu.be':
		 $link = 'https://www.youtube.com/embed/'.$id;
		 break;

	 default:
		 return false;
 }

 return $link;
}

}