Skip to content

Instantly share code, notes, and snippets.

@vincentorback
Last active November 29, 2021 09:53
Show Gist options
  • Save vincentorback/f3c3e1d8fac993316df7f07cb279c6dd to your computer and use it in GitHub Desktop.
Save vincentorback/f3c3e1d8fac993316df7f07cb279c6dd to your computer and use it in GitHub Desktop.
Get ID from videos embedded from YouTube and Vimeo
<?php
function getElementAttribute ($attribute, $html) {
if (preg_match('/' . $attribute . '="([^"]+)"/', $html, $match)) {
return $match[1];
}
return false;
}
function getVimeoId ($url) {
if (preg_match('#(?:https?://)?(?:www.)?(?:player.)?vimeo.com/(?:[a-z]*/)*([0-9]{6,11})[?]?.*#', $url, $match)) {
return $match[1];
}
return false;
}
function getYoutubeId ($url) {
if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/\s]{11})%i', $url, $match)) {
return $match[1];
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment