Skip to content

Instantly share code, notes, and snippets.

@ritxi
Created February 25, 2011 19:37
Show Gist options
  • Select an option

  • Save ritxi/844364 to your computer and use it in GitHub Desktop.

Select an option

Save ritxi/844364 to your computer and use it in GitHub Desktop.
transform a youtube url into a embed youtube video
def embed_url(string)
(match = /^(http|https)\:\/\/www\.youtube\.com\/watch\?v\=(\w*)(\&(.*))?$/i.match(string)) ? "http://www.youtube.com/e/#{match[2]}" : nil
end
puts embed_url( "http://www.youtube.com/watch?v=ONvTNAG2e9c&feature=related") == "http://www.youtube.com/e/ONvTNAG2e9c"
puts embed_url("http://www.youtube.com/watch?v=ONvTNAG2e9c") == "http://www.youtube.com/e/ONvTNAG2e9c"
puts embed_url("http://www.hola.com/watch?v=ONvTNAG2e9c") == "http://www.hola.com/e/ONvTNAG2e9c"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment