Skip to content

Instantly share code, notes, and snippets.

@chriswagoner
Created June 2, 2021 18:54
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 chriswagoner/486e932691d8d2c2cd5f64f216538216 to your computer and use it in GitHub Desktop.
Save chriswagoner/486e932691d8d2c2cd5f64f216538216 to your computer and use it in GitHub Desktop.
Convert YouTube URLs to Embed URLs
function go_convert_youtube_url_to_embed_url($youtube_url) {
$matches = array();
preg_match('/((?:https?:)?\/\/)?((?:www|m)\.)?((?:youtube\.com|youtu.be))(\/(?:[\w\-]+\?v=|embed\/|v\/)?)([\w\-]+)(\S+)?/', $youtube_url, $matches);
return "https://www.youtube.com/embed/".$matches[5];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment