Can someone show me how to get the youtube id out of a url regardless of what other GET variables are in the URL.
Use this video for example: http://www.youtube.com/watch?v=C4kxS1ksqtw&feature=related
So between v= and before the next &
Can someone show me how to get the youtube id out of a url regardless of what other GET variables are in the URL.
Use this video for example: http://www.youtube.com/watch?v=C4kxS1ksqtw&feature=related
So between v= and before the next &
The following will work for all youtube links
<?php
// Here is a sample of the URLs this regex matches: (there can be more content after the given URL that will be ignored)
// http://youtu.be/dQw4w9WgXcQ
// http://www.youtube.com/embed/dQw4w9WgXcQ
// http://www.youtube.com/watch?v=dQw4w9WgXcQ
// http://www.youtube.com/?v=dQw4w9WgXcQ
// http://www.youtube.com/v/dQw4w9WgXcQ
// http://www.youtube.com/e/dQw4w9WgXcQ
// http://www.youtube.com/user/username#p/u/11/dQw4w9WgXcQ
// http://www.youtube.com/sandalsResorts#p/c/54B8C800269D7C1B/0/dQw4w9WgXcQ
// http://www.youtube.com/watch?feature=player_embedded&v=dQw4w9WgXcQ
// http://www.youtube.com/?feature=player_embedded&v=dQw4w9WgXcQ
// It also works on the youtube-nocookie.com URL with the same above options.
// It will also pull the ID from the URL in an embed code (both iframe and object tags)
$url = "https://www.youtube.com/watch?v=v2_MLFVdlQM";
preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match);
$youtube_id = $match[1];
echo $youtube_id;
?>
For extracting the id in a capturing group, the following expression or some derivative of that might be an option too:
(?im)\b(?:https?:\/\/)?(?:w{3}\.)?youtu(?:be)?\.(?:com|be)\/(?:(?:\??v=?i?=?\/?)|watch\?vi?=|watch\?.*?&v=|embed\/|)([A-Z0-9_-]{11})\S*(?=\s|$)
$re = '/(?im)\b(?:https?:\/\/)?(?:w{3}\.)?youtu(?:be)?\.(?:com|be)\/(?:(?:\??v=?i?=?\/?)|watch\?vi?=|watch\?.*?&v=|embed\/|)([A-Z0-9_-]{11})\S*(?=\s|$)/';
$str = 'http://youtube.com/v/tFad5gHoBjY
https://youtube.com/vi/tFad5gHoBjY
http://www.youtube.com/?v=tFad5gHoBjY
http://www.youtube.com/?vi=tFad5gHoBjY
https://www.youtube.com/watch?v=tFad5gHoBjY
youtube.com/watch?vi=tFad5gHoBjY
youtu.be/tFad5gHoBjY
http://youtu.be/qokEYBNWA_0?t=30m26s
youtube.com/v/7HCZvhRAk-M
youtube.com/vi/7HCZvhRAk-M
youtube.com/?v=7HCZvhRAk-M
youtube.com/?vi=7HCZvhRAk-M
youtube.com/watch?v=7HCZvhRAk-M
youtube.com/watch?vi=7HCZvhRAk-M
youtu.be/7HCZvhRAk-M
youtube.com/embed/7HCZvhRAk-M
http://youtube.com/v/7HCZvhRAk-M
http://www.youtube.com/v/7HCZvhRAk-M
https://www.youtube.com/v/7HCZvhRAk-M
youtube.com/watch?v=7HCZvhRAk-M&wtv=wtv
http://www.youtube.com/watch?dev=inprogress&v=7HCZvhRAk-M&feature=related
youtube.com/watch?v=7HCZvhRAk-M
http://youtube.com/v/dQw4w9WgXcQ?feature=youtube_gdata_player
http://youtube.com/vi/dQw4w9WgXcQ?feature=youtube_gdata_player
http://youtube.com/?v=dQw4w9WgXcQ&feature=youtube_gdata_player
http://www.youtube.com/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player
http://youtube.com/?vi=dQw4w9WgXcQ&feature=youtube_gdata_player
http://youtube.com/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player
http://youtube.com/watch?vi=dQw4w9WgXcQ&feature=youtube_gdata_player
http://youtu.be/dQw4w9WgXcQ?feature=youtube_gdata_player';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
var_dump($matches);
array(30) {
[0]=>
array(2) {
[0]=>
string(32) "http://youtube.com/v/tFad5gHoBjY"
[1]=>
string(11) "tFad5gHoBjY"
}
[1]=>
array(2) {
[0]=>
string(34) "https://youtube.com/vi/tFad5gHoBjY"
[1]=>
string(11) "tFad5gHoBjY"
}
[2]=>
array(2) {
[0]=>
string(37) "http://www.youtube.com/?v=tFad5gHoBjY"
[1]=>
string(11) "tFad5gHoBjY"
}
[3]=>
array(2) {
[0]=>
string(38) "http://www.youtube.com/?vi=tFad5gHoBjY"
[1]=>
string(11) "tFad5gHoBjY"
}
[4]=>
array(2) {
[0]=>
string(43) "https://www.youtube.com/watch?v=tFad5gHoBjY"
[1]=>
string(11) "tFad5gHoBjY"
}
[5]=>
array(2) {
[0]=>
string(32) "youtube.com/watch?vi=tFad5gHoBjY"
[1]=>
string(11) "tFad5gHoBjY"
}
[6]=>
array(2) {
[0]=>
string(20) "youtu.be/tFad5gHoBjY"
[1]=>
string(11) "tFad5gHoBjY"
}
[7]=>
array(2) {
[0]=>
string(27) "http://youtu.be/qokEYBNWA_0"
[1]=>
string(11) "qokEYBNWA_0"
}
[8]=>
array(2) {
[0]=>
string(25) "youtube.com/v/7HCZvhRAk-M"
[1]=>
string(11) "7HCZvhRAk-M"
}
[9]=>
array(2) {
[0]=>
string(26) "youtube.com/vi/7HCZvhRAk-M"
[1]=>
string(11) "7HCZvhRAk-M"
}
[10]=>
array(2) {
[0]=>
string(26) "youtube.com/?v=7HCZvhRAk-M"
[1]=>
string(11) "7HCZvhRAk-M"
}
[11]=>
array(2) {
[0]=>
string(27) "youtube.com/?vi=7HCZvhRAk-M"
[1]=>
string(11) "7HCZvhRAk-M"
}
[12]=>
array(2) {
[0]=>
string(31) "youtube.com/watch?v=7HCZvhRAk-M"
[1]=>
string(11) "7HCZvhRAk-M"
}
[13]=>
array(2) {
[0]=>
string(32) "youtube.com/watch?vi=7HCZvhRAk-M"
[1]=>
string(11) "7HCZvhRAk-M"
}
[14]=>
array(2) {
[0]=>
string(20) "youtu.be/7HCZvhRAk-M"
[1]=>
string(11) "7HCZvhRAk-M"
}
[15]=>
array(2) {
[0]=>
string(29) "youtube.com/embed/7HCZvhRAk-M"
[1]=>
string(11) "7HCZvhRAk-M"
}
[16]=>
array(2) {
[0]=>
string(32) "http://youtube.com/v/7HCZvhRAk-M"
[1]=>
string(11) "7HCZvhRAk-M"
}
[17]=>
array(2) {
[0]=>
string(36) "http://www.youtube.com/v/7HCZvhRAk-M"
[1]=>
string(11) "7HCZvhRAk-M"
}
[18]=>
array(2) {
[0]=>
string(37) "https://www.youtube.com/v/7HCZvhRAk-M"
[1]=>
string(11) "7HCZvhRAk-M"
}
[19]=>
array(2) {
[0]=>
string(31) "youtube.com/watch?v=7HCZvhRAk-M"
[1]=>
string(11) "7HCZvhRAk-M"
}
[20]=>
array(2) {
[0]=>
string(57) "http://www.youtube.com/watch?dev=inprogress&v=7HCZvhRAk-M"
[1]=>
string(11) "7HCZvhRAk-M"
}
[21]=>
array(2) {
[0]=>
string(31) "youtube.com/watch?v=7HCZvhRAk-M"
[1]=>
string(11) "7HCZvhRAk-M"
}
[22]=>
array(2) {
[0]=>
string(32) "http://youtube.com/v/dQw4w9WgXcQ"
[1]=>
string(11) "dQw4w9WgXcQ"
}
[23]=>
array(2) {
[0]=>
string(33) "http://youtube.com/vi/dQw4w9WgXcQ"
[1]=>
string(11) "dQw4w9WgXcQ"
}
[24]=>
array(2) {
[0]=>
string(33) "http://youtube.com/?v=dQw4w9WgXcQ"
[1]=>
string(11) "dQw4w9WgXcQ"
}
[25]=>
array(2) {
[0]=>
string(42) "http://www.youtube.com/watch?v=dQw4w9WgXcQ"
[1]=>
string(11) "dQw4w9WgXcQ"
}
[26]=>
array(2) {
[0]=>
string(34) "http://youtube.com/?vi=dQw4w9WgXcQ"
[1]=>
string(11) "dQw4w9WgXcQ"
}
[27]=>
array(2) {
[0]=>
string(38) "http://youtube.com/watch?v=dQw4w9WgXcQ"
[1]=>
string(11) "dQw4w9WgXcQ"
}
[28]=>
array(2) {
[0]=>
string(39) "http://youtube.com/watch?vi=dQw4w9WgXcQ"
[1]=>
string(11) "dQw4w9WgXcQ"
}
[29]=>
array(2) {
[0]=>
string(27) "http://youtu.be/dQw4w9WgXcQ"
[1]=>
string(11) "dQw4w9WgXcQ"
}
}
If you wish to simplify/modify/explore the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.
jex.im visualizes regular expressions:
$vid = preg_replace('/^.*(\?|\&)v\=/', '', $url); // Strip all meuk before and including '?v=' or '&v='.
$vid = preg_replace('/[^\w\-\_].*$/', '', $vid); // Strip trailing meuk.
Use this code :
$url = "http://www.youtube.com/watch?v=C4kxS1ksqtw&feature=related";
$parse = parse_url($url, PHP_URL_QUERY);
parse_str($parse, $output);
echo $output['watch'];
result : C4kxS1ksqtw
Just found this online at http://snipplr.com/view/62238/get-youtube-video-id-very-robust/
function getYouTubeId($url) {
// Format all domains to http://domain for easier URL parsing
str_replace('https://', 'http://', $url);
if (!stristr($url, 'http://') && (strlen($url) != 11)) {
$url = 'http://' . $url;
}
$url = str_replace('http://www.', 'http://', $url);
if (strlen($url) == 11) {
$code = $url;
} else if (preg_match('/http:\/\/youtu.be/', $url)) {
$url = parse_url($url, PHP_URL_PATH);
$code = substr($url, 1, 11);
} else if (preg_match('/watch/', $url)) {
$arr = parse_url($url);
parse_str($url);
$code = isset($v) ? substr($v, 0, 11) : false;
} else if (preg_match('/http:\/\/youtube.com\/v/', $url)) {
$url = parse_url($url, PHP_URL_PATH);
$code = substr($url, 3, 11);
} else if (preg_match('/http:\/\/youtube.com\/embed/', $url, $matches)) {
$url = parse_url($url, PHP_URL_PATH);
$code = substr($url, 7, 11);
} else if (preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=[0-9]/)[^&\n]+|(?<=v=)[^&\n]+#", $url, $matches) ) {
$code = substr($matches[0], 0, 11);
} else {
$code = false;
}
if ($code && (strlen($code) < 11)) {
$code = false;
}
return $code;
}
I used the data from Shawn's answer but generalized and shortened the regex a little. The key difference with this one is that it won't test for a valid Youtube URL, it'll just look for a video ID. Meaning it will still return a video ID for www.facebook.com?wtv=youtube.com/v/vidid. Works for all the test cases, but is a bit more lax. Consequently, it will output a false positive for something like https://www.twitter.com/watch?v=vidid. Use this method if the data is super inconsistent, otherwise use a more specific regex or parse_url() and parse_str().
preg_match("/([\?&\/]vi?|embed|\.be)[\/=]([\w-]+)/",$url,$matches);
print($matches[2]);
I think you are trying to do this.
<?php
$video = 'https://www.youtube.com/watch?v=u00FY9vADfQ';
$parsed_video = parse_url($video, PHP_URL_QUERY);
parse_str($parsed_video, $arr);
?>
<iframe
src="https://www.youtube.com/embed/<?php echo $arr['v']; ?>"
frameborder="0">
</iframe>
and what if I want to extract a youtue url from a string full of other characters? like this:
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco https://www.youtube.com/watch?v=cPW9Y94BJI0 laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
and get https://www.youtube.com/watch?v=cPW9Y94BJI0 from that string?