Check video embed for comment posts

Embed video to comment or post

	function check_embed($source, $width = 455, $height = 300) 
	{
		$source = strtolower($source);
		$embed_code = '';
		$video = false;

		if(preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+(?=\?)|(?<=v=)[^&\n]+|(?<=youtu.be/)[^&\n]+#", $source, $matches))
		{
			$video = true;
			$url_id = $matches[0];
			
			$embed_code .= '<object type="application/x-shockwave-flash" style="width:'.$width.'px; height:'.$height.'px;" data="http://www.youtube.com/v/'.$url_id.'">';
			$embed_code .= '<param name="movie" value="http://www.youtube.com/v/'.$url_id.'" />';
			$embed_code .= '</object>';
		} 
		elseif(preg_match('/^http\:\/\/www\.myspace\.com/', $source)) 
		{
			if(preg_match('/video\/vid\/([0-9a-zA-Z-_]*)/', $source, $matches))
			{
				$video = true;
				$url_id = $matches[1];
				
				$embed_code .= '<object width="'.$width.'" height="'.$height.'" >';
					$embed_code .= '<param name="allowFullScreen" value="true"/>';
					$embed_code .= '<param name="movie" value="http://mediaservices.myspace.com/services/media/embed.aspx/m='.$url_id.',t=1,mt=video"/>';
					$embed_code .= '<embed src="http://mediaservices.myspace.com/services/media/embed.aspx/m='.$url_id.',t=1,mt=video" width="'.$width.'" height="'.$height.'" allowFullScreen="true" type="application/x-shockwave-flash"></embed>';
				$embed_code .= '</object>';
			}
		}
		elseif(preg_match('/\<embed\s+.*src\=\"?[^\"]*mediaservices\.myspace\.com\/services\/media\/embed.aspx\/m=(.+)\??.*\".*\<\/embed\>/i', stripslashes($source), $matches))
		{
			$video = true;
			$get_url_id = explode(',', $matches[1]);
			$url_id = $get_url_id[0];
			
			$embed_code .= '<object width="'.$width.'" height="'.$height.'" >';
				$embed_code .= '<param name="allowFullScreen" value="true"/>';
				$embed_code .= '<param name="movie" value="http://mediaservices.myspace.com/services/media/embed.aspx/m='.$url_id.',t=1,mt=video"/>';
				$embed_code .= '<embed src="http://mediaservices.myspace.com/services/media/embed.aspx/m='.$url_id.',t=1,mt=video" width="'.$width.'" height="'.$height.'" allowFullScreen="true" type="application/x-shockwave-flash"></embed>';
			$embed_code .= '</object>';
		}
		elseif(preg_match('/^http\:\/\/vimeo\.com/', $source))
		{
			if(preg_match('/^http\:\/\/vimeo.com\/([0-9a-zA-Z-_]*)/', $source, $matches))
			{
				$video = true;
				$url_id = $matches[1];
				
				$embed_code .= '<iframe src="http://player.vimeo.com/video/'.$url_id.'" width="'.$width.'" height="'.$height.'" frameborder="0"></iframe>';
			}	
		}
		elseif(preg_match('/\<iframe\s+.*src\=\"?[^\"]*player\.vimeo\.com\/video\/([\d]{5,})\??.*\".*\<\/iframe\>/i', stripslashes($source), $matches))
		{
			$video = true;
			$url_id = $matches[1];
				
			$embed_code .= '<iframe src="http://player.vimeo.com/video/'.$url_id.'" width="'.$width.'" height="'.$height.'" frameborder="0"></iframe>';
		}
		elseif(preg_match('/^http\:\/\/player\.vimeo\.com/', $source))
		{
			if(preg_match('/video\/([0-9a-zA-Z-_]*)/', $source, $matches))
			{
				$video = true;
				$url_id = $matches[1];
				echo $url_id;
				$embed_code .= '<iframe src="http://player.vimeo.com/video/'.$url_id.'" width="'.$width.'" height="'.$height.'" frameborder="0"></iframe>';
			}	
		}
		elseif(preg_match('/^http\:\/\/screen\.yahoo\.com/', $source) || preg_match('/^http://screen.yahoo.com/(\d+)/', $source))
		{
			if(preg_match("/-(.*?)\./s",$source, $matches))
			{
				$video = true;
				$url_id = explode('-', $matches[1]);
				$last_item = sizeof($url_id)-1;
				$embed_code .= '<iframe frameborder="0" width="'.$width.'" height="'.$height.'" src="http://d.yimg.com/nl/vyc/site/player.html#vid='.$url_id[$last_item].'&shareUrl=http://screen.yahoo.com/'.$matches[1].'-'.$url_id[$last_item].'.html&browseCarouselUI=hide&repeat=0&startScreenCarouselUI=hide"></iframe>';
			}
		}
		elseif(preg_match('/^http\:\/\/www\.flickr\.com/', $source))
		{
			if(preg_match('/photos\/[0-9a-zA-Z-_]*\/([0-9a-zA-Z-_]*)/', $source, $matches))
			{
				$video = true;
				$url_id = $matches[1];
				
				$embed_code .= '<object type="application/x-shockwave-flash" width="'.$width.'" height="'.$height.'" data="http://www.flickr.com/apps/video/stewart.swf" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"> ';
				$embed_code .= '<embed type="application/x-shockwave-flash" src="http://www.flickr.com/apps/video/stewart.swf" allowfullscreen="true" flashvars="photo_id='.$url_id.'" height="'.$height.'" width="'.$width.'"></embed></object>';
			}
		} 
		elseif(preg_match('/^http\:\/\/soundcloud.com/', $source))
		{
			if(preg_match('/^http\:\/\/soundcloud.com\/.*/', $source))
			{
				$video = true;
				$embed_code .= '<object height="81" width="'.$width.'px"> <param name="movie" value="http://player.soundcloud.com/player.swf?url='.$source.'&show_comments=false"></param> ';
				$embed_code .= '<embed height="81" src="http://player.soundcloud.com/player.swf?url='.$source.'&show_comments=false" type="application/x-shockwave-flash" width="'.$width.'px"></embed></object>';
			} 
			elseif(preg_match('/http://api.soundcloud.com/tracks/(\d+)/', $source, $matches)) 
			{
				$video = true;
				$url_id = $matches[1];
				
				$embed_code .= '<object height="81" width="'.$width.'px"><param name="movie" value="http://player.soundcloud.com/player.swf?url=http://w.soundcloud.com/player/?url=http://api.soundcloud.com/tracks/'.$url_id.'&show_comments=false"></param> ';
				$embed_code .= '<embed height="81" src="http://w.soundcloud.com/player/?url=http://api.soundcloud.com/tracks/'.$url_id.'&show_comments=false" type="application/x-shockwave-flash" width="'.$width.'px"></embed></object>';
			}
		}
		
		if($video == true)
		{
			return $embed_code;
		} else {
			return $source;
                        // dont forget to real_escape later ;-)
		}
	}

Usage

Just add the function to your comment post, like check_embed($_POST['comment']);


Comments

Add your comment