<?php
class YouTube{

	private $id = NULL;	

	function __construct($url = NULL){
		
			if ($url != null){
				$this->id = YouTube::parseURL($url);
			}
		
	}//end __construct()

	public function setID($id){
		
			if (preg_match('/([A-Za-z0-9_-]+)/', $url, $matches)){
				$this->id = $id;
				return true;
			}
			else{
				return false;
			}
			
	}//end function setID(

	public function getID(){
			
			return $this->id;
	}	

	public static function parseURL($url){
			
			if (preg_match('/watch\?v\=([A-Za-z0-9_-]+)/', $url, $matches)){
				return $matches[1];
			}
			else{
				return false;
			}
			
	}//end function parseURL($url)
	
	public function EmbedVideo($url = null, $width = 425, $height = 344) {
			
			if ($url == null){
				$videoid = $this->id;
			}
			else{
				$videoid = YouTube::parseURL($url);
				if (!$videoid) $videoid = $url;
			}

		return '<object width="'.$width.'" height="'.$height.'"><param name="movie" value="http://www.youtube.com/v/'.$videoid.'?rel=0&fs=1&loop=0"></param><param name="wmode" value="transparent"></param><param name="allowFullScreen" value="true"><embed src="http://www.youtube.com/v/'.$videoid.'?rel=0&fs=1&loop=0" allowfullscreen="true" type="application/x-shockwave-flash" wmode="transparent" width="'.$width.'" height="'.$height.'"></embed></object>';
		
	}//end EmbedVideo()

	public function GetImgURL($url = NULL, $imgid = 1){
			
			if ($url == null){
				$videoid = $this->id;
			}
			else{
				$videoid = YouTube::parseURL($url);
				if (!$videoid){
					$videoid = $url;
				}
			}

		return "http://img.youtube.com/vi/$videoid/$imgid.jpg";
	
	}//end function GetImgURL()

	public function GetImg($url = null, $imgid = 1){
		
		return GetImgURL($url, $imgid);
	}
	
	public function ShowImg($url = null, $imgid = 1, $alt = 'Video screenshot'){
		return "<img src='".$this->GetImgURL($url, $imgid)."' width='130' height='97' border='0' alt='".$alt."' title='".$alt."' />";
	}
			
	public function ShowTitle($url = null){
		 $query = @file_get_contents($url);
		 $exp_info = "!<meta name=\"title\" content=\"(.+)\">!U";
		 preg_match_all($exp_info,$query,$titulo); 
		 $titulo   = $titulo[1][0]; 
		 return $titulo;
	}	

	public function ShowDesc($url = null){
		 
		  $query = @file_get_contents($url);

				$exp_info = "!<meta name=\"description\" content=\"(.+)\">!U";
				preg_match_all($exp_info,$query,$descripcion); 
				$descripcion    = $descripcion[1][0];
		 
		 return $descripcion;		 
	}//end function ShowDesc()

}//end class YouTube()
?>