有人可以向我展示如何从URL中获取YouTube ID,而不管URL中包含了什么其他GET变量。
以此视频为例:http://www.youtube.com/watch?v=C4kxS1ksqtw&feature=related
因此在v=
和下一个&
之间就是YouTube ID。
if (preg_match('![?&]{1}v=([^&]+)!', $url . '&', $m))
$video_id = $m[1];
我有一些文章内容需要解密,以获取Youtube视频ID。它恰好是由Youtube提供的<iframe>
嵌入代码形式。
<iframe src="http://www.youtube.com/embed/Zpk8pMz_Kgw?rel=0" frameborder="0" width="620" height="360"></iframe>
$pattern = '#(?<=(?:v|i)=)[a-zA-Z0-9-]+(?=&)|(?<=(?:v|i)\/)[^&\n]+|(?<=embed\/)[^"&\n]+|(?<=(?:v|i)=)[^&\n]+|(?<=youtu.be\/)[^&\n]+#';
preg_match_all($pattern, $post_content, $matches);
foreach ($matches as $match) {
$img = "<img src='http://img.youtube.com/vi/".str_replace('?rel=0','', $match[0])."/0.jpg' />";
break;
}
Rob的个人资料: https://stackoverflow.com/users/149615/rob
$vid = preg_replace('/^.*(\?|\&)v\=/', '', $url); // Strip all meuk before and including '?v=' or '&v='.
$vid = preg_replace('/[^\w\-\_].*$/', '', $vid); // Strip trailing meuk.
我知道这个帖子的标题涉及到正则表达式的使用,但正如Zawinski所说,我认为在这里避免使用正则表达式是最好的选择。我建议使用这个函数代替:
function get_youtube_id($url)
{
if (strpos( $url,"v=") !== false)
{
return substr($url, strpos($url, "v=") + 2, 11);
}
elseif(strpos( $url,"embed/") !== false)
{
return substr($url, strpos($url, "embed/") + 6, 11);
}
}
http://www.youtube.com/watch?v=t_uW44Bsezg
http://www.youtube.com/watch?feature=endscreen&v=Id3xG4xnOfA&NR=1
iframe
的内容。使用以下代码:
$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
刚在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;
}
如果我想从一个充满其他字符的字符串中提取YouTube网址怎么办? 例如:
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 ullamcohttps://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。
如何从该字符串中获取https://www.youtube.com/watch?v=cPW9Y94BJI0?
www.facebook.com?wtv=youtube.com/v/vidid
的视频ID。适用于所有测试用例,但要宽松一些。因此,对于像https://www.twitter.com/watch?v=vidid
这样的内容,它将输出虚假信息。如果数据非常不一致,请使用此方法,否则请使用更具体的正则表达式或 parse_url()
和 parse_str()
。preg_match("/([\?&\/]vi?|embed|\.be)[\/=]([\w-]+)/",$url,$matches);
print($matches[2]);
我想你是在尝试这样做。
<?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>
$ch=curl_init(); curl_setopt($ch, CURLOPT_URL, "http://vimeo.com/api/v2/video/$id.php"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); $hash =unserialize(curl_exec($ch)); curl_close($ch);
- jerrygarciuh