Advertisement
peterurfi

class.YoutubeUrl

Mar 9th, 2011
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.80 KB | None | 0 0
  1. <?php
  2.  
  3. class YoutubeUrl
  4. {
  5.     private static $isValid = false;
  6.  
  7.     /**
  8.      * Returns with the Youtube video ID from the full URL.
  9.      *
  10.      * @param string $url
  11.      * @return string
  12.      */
  13.     public function getID($url)
  14.     {
  15.  
  16.         if(self::$isValid == false) { exit(); }
  17.         if(!strpos($url, "#!v=") === false) { $url = str_replace("#!v=", "?v=", $url); }
  18.  
  19.         parse_str(parse_url($url, PHP_URL_QUERY));
  20.  
  21.         if(isset($v)) { return $v; }
  22.         else { return substr($url, strrpos($url, "/") +1, 11); }
  23.     }
  24.  
  25.     /**
  26.      * Generates the proper Youtube link to the video from the id
  27.      *
  28.      * @param string $id
  29.      * @return string
  30.      */
  31.     public function generateLink($id)
  32.     {
  33.         if(self::$isValid == false) { exit(); }
  34.         $generatedLink = "http://www.youtube.com/watch?v=" . trim($id);
  35.         return $generatedLink;
  36.     }
  37.  
  38.     /**
  39.      * Creates the embed code for the video
  40.      *
  41.      * @param string $id video id
  42.      * @param int $width playbackframe width (def: 320)
  43.      * @param int $height playbackframe height (def: 240)
  44.      * @return string
  45.      */
  46.     public function generateEmbedded($id, $width=320, $height=240)
  47.     {
  48.         if(self::$isValid == false) { exit(); }
  49.        
  50.         $embedCode =  '<object width="' . $width . '" height="' . $height . '">';
  51.         $embedCode .= '<param name="movie" value="http://www.youtube.com/v/' . $id . '?fs=1&hl=pt_BR&rel=0"></param>';
  52.         $embedCode .= '<param name="allowFullScreen" value="true"></param>';
  53.         $embedCode .= '<param name="allowscriptaccess" value="always"></param>';
  54.         $embedCode .= '<embed src="http://www.youtube.com/v/' . $id . '?fs=1&hl=pt_BR&rel=0"';
  55.         $embedCode .= 'type="application/x-shockwave-flash"';
  56.         $embedCode .= 'allowscriptaccess="always"';
  57.         $embedCode .= 'allowfullscreen="true"';
  58.         $embedCode .= 'width="' . $width . '"';
  59.         $embedCode .= 'height="' . $height . '">';
  60.         $embedCode .= '</embed>';
  61.         $embedCode .= '</object>';
  62.  
  63.         return $embedCode;
  64.     }
  65.  
  66.     /**
  67.      * Creates the embed code
  68.      *
  69.      * @param string $url
  70.      * @param int $width
  71.      * @param int $height
  72.      */
  73.     public function url2Embedded($url, $width=320, $height=240)
  74.     {
  75.         if(self::$isValid == false) { exit(); }
  76.        
  77.         echo $this->generateEmbedded($this->getID($url), $width, $height);
  78.     }
  79.  
  80.     /**
  81.      * Validates the Youtube video URL
  82.      *
  83.      * @param string $url YT video url
  84.      */
  85.     public function validateYtUrl($url)
  86.     {
  87.         $url = substr($url, 0, 42);
  88.         $p_url = parse_url($url);
  89.        
  90.         if(empty($p_url['scheme'])) { $scheme = ""; self::$isValid = false; }
  91.         else { $scheme = $p_url['scheme']; }
  92.  
  93.         if(empty($p_url['host'])) { $host = ""; self::$isValid = false; }
  94.         else { $host = $p_url['host']; }
  95.  
  96.         if(empty($p_url['path'])) { $path = ""; self::$isValid = false; }
  97.         else { $path = $p_url['path']; }
  98.  
  99.         if(empty($p_url['query'])) { $query = ""; self::$isValid = false; }
  100.         else { $query = $p_url['query']; }
  101.  
  102.         if($scheme != "http" || $host != "www.youtube.com" || $path != "/watch" || strlen($query) != 13) { self::$isValid = false; }
  103.         else { self::$isValid = true; }
  104.  
  105.         return self::$isValid;
  106.     }
  107.  
  108.     /**
  109.      * Returns with the validity of the URL
  110.      *
  111.      * @return bool
  112.      */
  113.     public function isUrlValid()
  114.     {
  115.         if(self::$isValid == false) { return false; }
  116.         elseif(self::$isValid == true) { return true; }
  117.     }
  118.  
  119.     public function ytUrlParser($url)
  120.     {
  121.         $url = substr($url, 0, 42);
  122.         $p_url = parse_url($url);
  123.         echo substr($p_url['query'], 0, 13) . '<br />';
  124.         print_r($p_url);
  125.     }
  126. }
  127.  
  128. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement