Created
October 7, 2013 14:08
-
-
Save bordoni/6868616 to your computer and use it in GitHub Desktop.
Functions to use Youtube
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function youtube_image_url($id, $which=false){ | |
| $server = rand(1,5); | |
| if ($server===5) { | |
| $url = "http://img.youtube.com/vi/{$id}/"; | |
| } else { | |
| $url = "http://i{$server}.ytimg.com/vi/{$id}/"; | |
| } | |
| switch ($which) { | |
| case 0: | |
| $url .= "0.jpg"; | |
| break; | |
| case 1: | |
| $url .= "1.jpg"; | |
| break; | |
| case 2: | |
| $url .= "2.jpg"; | |
| break; | |
| case 3: | |
| $url .= "3.jpg"; | |
| break; | |
| case 'med': | |
| $url .= "mqdefault.jpg"; | |
| break; | |
| case 'high': | |
| $url .= "hqdefault.jpg"; | |
| break; | |
| case 'max': | |
| $url .= "maxresdefault.jpg"; | |
| break; | |
| default: | |
| $url .= "default.jpg"; | |
| break; | |
| } | |
| return $url; | |
| } | |
| function get_youtube_id( $fonte='' ) { | |
| preg_match( '~ | |
| # Match non-linked youtube URL in the wild. (Rev:20110825) | |
| https?:// # Required scheme. Either http or https. | |
| (?:www\.)? # Optional www subdomain. | |
| (?: # Group host alternatives. | |
| youtu\.be/ # Either youtu.be, | |
| | youtube\.com # or youtube.com followed by | |
| \S* # Allow anything up to VIDEO_ID, | |
| [^\w\-\s] # but char before ID is non-ID char. | |
| ) # End host alternatives. | |
| v=([\w\-]{11}) # $1: VIDEO_ID is exactly 11 chars, and is on the "v" arg | |
| (?=[^\w\-]|$) # Assert next char is non-ID or EOS. | |
| (?! # Assert URL is not pre-linked. | |
| [?=&+%\w]* # Allow URL (query) remainder. | |
| (?: # Group pre-linked alternatives. | |
| [\'"][^<>]*> # Either inside a start tag, | |
| | </a> # or inside <a> element text contents. | |
| ) # End recognized pre-linked alts. | |
| ) # End negative lookahead assertion. | |
| [?=&+%\w]* # Consume any URL (query) remainder. | |
| ~ix', $fonte, $youtube ); | |
| if(!empty($youtube[0])) | |
| return $youtube[1]; | |
| else | |
| return false; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment