Created
March 12, 2018 21:26
-
-
Save robinbastien/c9a69cba7b7d04148a33ae2673ca502a to your computer and use it in GitHub Desktop.
[Youtube Embed Helpers] Functions to convert a youtube URL into a Youtube Embed URL #youtube #PHP #embed
This file contains 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 | |
/** | |
* Retrieves ID from Youtube URL | |
* @param [type] $url The full youtube URL | |
* @return [type] Just the ID | |
*/ | |
function get_youtube_id_from_url($url) { | |
$parts = parse_url($url); | |
parse_str($parts['query'], $query); | |
return $query['v'] ?? false; | |
} | |
/** | |
* Converts youtube URL into embeddable URL | |
* @param [type] $url The full youtube URL | |
* @return [type] Just the ID | |
*/ | |
function get_youtube_embed_url( $url = false ) { | |
if( $url ) { | |
$id = get_youtube_id_from_url($url); | |
return 'https://www.youtube.com/embed/' . $id . '?rel=0&controls=0'; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment