Skip to content

Instantly share code, notes, and snippets.

@robinbastien
Created March 12, 2018 21:26
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 robinbastien/c9a69cba7b7d04148a33ae2673ca502a to your computer and use it in GitHub Desktop.
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
<?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&amp;controls=0';
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment