Skip to content

Instantly share code, notes, and snippets.

@ahmednuaman
Created July 16, 2010 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmednuaman/478321 to your computer and use it in GitHub Desktop.
Save ahmednuaman/478321 to your computer and use it in GitHub Desktop.
public function check_youtube_url($s, $c=FALSE)
{
$page = 'youtube.com/watch?';
$player = 'youtube.com/v/';
$short = 'youtu.be/';
$clean = str_replace( array( 'http://', 'www.' ), '', $s );
if ( strpos( $clean, $page ) === 0 )
{
$vars_string = explode( '?', $s );
$vars_string = explode( '&', $vars_string[ 1 ] );
foreach ( $vars_string as $var )
{
$var = explode( '=', $var );
$vars[ $var[ 0 ] ] = $var[ 1 ];
}
$id = $vars[ 'v' ];
}
elseif ( strpos( $clean, $player ) === 0 )
{
$id = explode( '?', str_replace( $player, '', $clean ) );
$id = $id[ 0 ];
}
elseif ( strpos( $clean, $short ) === 0 )
{
$id = explode( '?', str_replace( $short, '', $clean ) );
$id = $id[ 0 ];
}
if ( $id )
{
$url = 'http://gdata.youtube.com/feeds/api/videos/' . $id . '?alt=json';
$data = @file_get_contents( $url );
if ( $data )
{
if ( $c )
{
return TRUE;
}
else
{
$json = json_decode( str_replace( '$t', 'text', $data ) );
return array( $id, (string)$json->entry->author[ 0 ]->name->text );
}
}
}
return FALSE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment