Last active
December 24, 2018 05:18
-
-
Save AlexanderPoellmann/9874e8ae081b677bf73d to your computer and use it in GitHub Desktop.
A simple PHP function to parse a given string for a YouTube or Vimeo link (or embed code) and extract video ID, provider, etc.
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 | |
/** | |
* Parse a video string | |
* | |
* @param string $haystack The string to be parsed | |
* @param string $protocol The protocol to be used for the urls | |
* @return array $video | |
* | |
* @version 2014-08-22 | |
* | |
* @author Poellmann Alexander Manfred, http://vendocr.at/ | |
* @copyright Copyright 2014 vendocrat. All Rights Reserved. | |
* @license MIT License | |
*/ | |
function vendocrat_parse_video( $haystack = '', $protocol = 'https' ) { | |
if ( empty($haystack) ) | |
return; | |
// make sure quotes (") are quotes and not enceded | |
$haystack = stripslashes( trim($haystack) ); | |
$haystack = htmlspecialchars_decode( $haystack ); | |
// check for iframe to get the video url | |
if ( strpos( $haystack, 'iframe' ) !== false ) { | |
// retrieve the video url | |
$pattern = '/src="(.*)?"/isU'; | |
if ( preg_match( $pattern, $haystack, $matches ) ) { | |
$link = trim( $matches[1] ); | |
} | |
} else { | |
// we already have a url | |
$link = $haystack; | |
} | |
// return if no link given | |
if ( empty($link) ) | |
return; | |
// prepare some vars | |
$provider = ''; | |
$video_id = false; | |
$video_str = ''; | |
$thumb_str = ''; | |
$image_str = ''; | |
/* | |
* Pattern tested on | |
* https://www.youtube.com/watch?v=M0jmSsQ5ptwhier | |
* https://www.youtube.com/v/M0jmSsQ5ptwhier?fs=1&hl=en_US | |
* https://www.youtube.com/embed/M0jmSsQ5ptwhier | |
* //www.youtube.com/embed/M0jmSsQ5ptwhier | |
* https://www.youtube.com/watch?v=M0jmSsQ5ptwhier | |
* http://player.vimeo.com/video/37985580?title=0&byline=0&portrait=0 | |
* http://vimeo.com/37985580 | |
*/ | |
$pattern = '@(http:\/\/|https:\/\/|\/\/)?(www|player)\.?(youtube|youtu|vimeo)\.(com|be)?\/(watch\?v=|v\/|embed\/|video\/)?([a-zA-Z0-9-_]+)@im'; | |
if ( preg_match( $pattern, $haystack, $matches ) ) { | |
$video_url = $matches[0]; | |
$video_protocol = $matches[1]; | |
$video_subdomain = $matches[2]; | |
$video_domain = $matches[3]; | |
$video_tld = $matches[4]; | |
$video_param = $matches[5]; | |
$video_id = $matches[6]; | |
} else { | |
return false; | |
} | |
// check protocol | |
if ( $protocol != 'http' AND $protocol != 'https' ) { | |
$protocol = 'https'; | |
} | |
$protocol.= ':'; | |
// YouTube | |
if ( strpos( $video_domain, 'youtu' ) !== false ) { | |
$provider = 'youtube'; | |
$video_str = $protocol .'//www.youtube.com/watch?v=%s'; | |
$thumb_str = $protocol .'//img.youtube.com/vi/%s/2.jpg'; | |
$image_str = $protocol .'//img.youtube.com/vi/%s/0.jpg'; | |
// Vimeo | |
} elseif ( strpos( $video_domain, 'vimeo' ) !== false ) { | |
$provider = 'vimeo'; | |
$video_str = $protocol .'//player.vimeo.com/video/%s'; | |
// try to get the thumbnail | |
try { | |
$hash = unserialize( @file_get_contents( "$protocol//vimeo.com/api/v2/video/$video_id.php" ) ); | |
if ( ! empty($hash) AND is_array($hash) ) { | |
$thumb_str = $hash[0]['thumbnail_small']; | |
$image_str = $hash[0]['thumbnail_large']; | |
} | |
} catch ( Exception $e ) {} | |
} | |
// set up arguments | |
$args = array( | |
'id' => $video_id, | |
'provider' => $provider, | |
'url' => sprintf( $video_str, $video_id ), | |
); | |
if ( $thumb_str ) $args['thumb'] = sprintf( $thumb_str, $video_id ); | |
if ( $image_str ) $args['image'] = sprintf( $image_str, $video_id ); | |
// parse arguments | |
$defaults = array( | |
'id' => false, | |
'provider' => false, | |
'url' => false, | |
'thumb' => false, | |
'image' => false, | |
); | |
$video = array_merge( $defaults, $args ); | |
return $video; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not working for this URL: https://vimeo.com/91993392