/ Published in: PHP

This will get the 11-character YouTube video ID from any valid YouTube URL.
Based on http://snipplr.com/view/57065/get-youtube-video-id/ but made more robust to handle additional URL inputs
Based on http://snipplr.com/view/57065/get-youtube-video-id/ but made more robust to handle additional URL inputs
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/** * Get 11-character YouTube video ID from a URL * * @param string $url URL to video * @return string|false False on invalid URL, 11 character string on success */ function getYouTubeId($url) { // Format all domains to http://domain for easier URL parsing $url = 'http://' . $url; } $code = $url; } else if (preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=[0-9]/)[^&\n]+|(?<=v=)[^&\n]+#", $url, $matches) ) { } else { $code = false; } $code = false; } return $code; }
Comments
