Skip to content

Instantly share code, notes, and snippets.

@heygambo
Last active December 20, 2015 16:59
Show Gist options
  • Select an option

  • Save heygambo/6165056 to your computer and use it in GitHub Desktop.

Select an option

Save heygambo/6165056 to your computer and use it in GitHub Desktop.
Find youtube id from any source in ruby (currently 4 types are supported)
def youtube_id(video_tag)
tests = []
tests.push(/embed\/([a-zA-Z0-9]+)/)
tests.push(/src="\/\/www.youtube.com\/v\/([a-zA-Z0-9]+)/)
tests.push(/youtu.be\/([a-zA-Z0-9]+)/)
tests.push(/youtube.com\/watch\?v=([a-zA-Z0-9]+)/)
tests.each do |regexp|
return video_tag.scan(regexp)[0][0] if video_tag.scan(regexp).length > 0
end
nil
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment