Skip to content

Instantly share code, notes, and snippets.

@gcoop
Created February 3, 2012 15:25
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 gcoop/1730737 to your computer and use it in GitHub Desktop.
Save gcoop/1730737 to your computer and use it in GitHub Desktop.
hacy way to get the video id from a path
<?php
$urls = array(
"http://vimeo.com/34633836",
"http://vimeo.com/34633836&dsfdsf&dhfjsdf=7fsdfjhdsk",
"http://www.youtube.com/watch?v=sYUKrUdYGHw&feature=youtu.be",
"http://www.youtube.com/watch?v=40F_kNd1VlY&feature=relmfu",
"http://www.youtube.com/watch?v=ia4O3CRSdDE&feature=g-all-u&context=G239539dFAAAAAAAAAAA"
);
function getId($str) {
if (strpos($str, "youtu") !== false) {
$parts = preg_split("/v=([a-zA-Z0-9_]*)/", $str, null, PREG_SPLIT_DELIM_CAPTURE);
return (isset($parts[1]) ? $parts[1] : false);
} else if (strpos($str, "vimeo") !== false) {
$parts = preg_split("/([0-9]{3,})/", $str, null, PREG_SPLIT_DELIM_CAPTURE);
return (isset($parts[1]) ? $parts[1] : false);
}
return false;
}
foreach ($urls as $url) {
echo("VIDEO ID: ".getId($url)."<br />");
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment