Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class YoutubeUrl
- {
- private static $isValid = false;
- /**
- * Returns with the Youtube video ID from the full URL.
- *
- * @param string $url
- * @return string
- */
- public function getID($url)
- {
- if(self::$isValid == false) { exit(); }
- if(!strpos($url, "#!v=") === false) { $url = str_replace("#!v=", "?v=", $url); }
- parse_str(parse_url($url, PHP_URL_QUERY));
- if(isset($v)) { return $v; }
- else { return substr($url, strrpos($url, "/") +1, 11); }
- }
- /**
- * Generates the proper Youtube link to the video from the id
- *
- * @param string $id
- * @return string
- */
- public function generateLink($id)
- {
- if(self::$isValid == false) { exit(); }
- $generatedLink = "http://www.youtube.com/watch?v=" . trim($id);
- return $generatedLink;
- }
- /**
- * Creates the embed code for the video
- *
- * @param string $id video id
- * @param int $width playbackframe width (def: 320)
- * @param int $height playbackframe height (def: 240)
- * @return string
- */
- public function generateEmbedded($id, $width=320, $height=240)
- {
- if(self::$isValid == false) { exit(); }
- $embedCode = '<object width="' . $width . '" height="' . $height . '">';
- $embedCode .= '<param name="movie" value="http://www.youtube.com/v/' . $id . '?fs=1&hl=pt_BR&rel=0"></param>';
- $embedCode .= '<param name="allowFullScreen" value="true"></param>';
- $embedCode .= '<param name="allowscriptaccess" value="always"></param>';
- $embedCode .= '<embed src="http://www.youtube.com/v/' . $id . '?fs=1&hl=pt_BR&rel=0"';
- $embedCode .= 'type="application/x-shockwave-flash"';
- $embedCode .= 'allowscriptaccess="always"';
- $embedCode .= 'allowfullscreen="true"';
- $embedCode .= 'width="' . $width . '"';
- $embedCode .= 'height="' . $height . '">';
- $embedCode .= '</embed>';
- $embedCode .= '</object>';
- return $embedCode;
- }
- /**
- * Creates the embed code
- *
- * @param string $url
- * @param int $width
- * @param int $height
- */
- public function url2Embedded($url, $width=320, $height=240)
- {
- if(self::$isValid == false) { exit(); }
- echo $this->generateEmbedded($this->getID($url), $width, $height);
- }
- /**
- * Validates the Youtube video URL
- *
- * @param string $url YT video url
- */
- public function validateYtUrl($url)
- {
- $url = substr($url, 0, 42);
- $p_url = parse_url($url);
- if(empty($p_url['scheme'])) { $scheme = ""; self::$isValid = false; }
- else { $scheme = $p_url['scheme']; }
- if(empty($p_url['host'])) { $host = ""; self::$isValid = false; }
- else { $host = $p_url['host']; }
- if(empty($p_url['path'])) { $path = ""; self::$isValid = false; }
- else { $path = $p_url['path']; }
- if(empty($p_url['query'])) { $query = ""; self::$isValid = false; }
- else { $query = $p_url['query']; }
- if($scheme != "http" || $host != "www.youtube.com" || $path != "/watch" || strlen($query) != 13) { self::$isValid = false; }
- else { self::$isValid = true; }
- return self::$isValid;
- }
- /**
- * Returns with the validity of the URL
- *
- * @return bool
- */
- public function isUrlValid()
- {
- if(self::$isValid == false) { return false; }
- elseif(self::$isValid == true) { return true; }
- }
- public function ytUrlParser($url)
- {
- $url = substr($url, 0, 42);
- $p_url = parse_url($url);
- echo substr($p_url['query'], 0, 13) . '<br />';
- print_r($p_url);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement