http://www.youtube.com/watch?v=6n8PGnc_cV4&feature=rec-LGOUT-real_rn-2r-13-HM
执行以下操作的最干净,最好的正则表达式是什么:
1。)我想在视频网址之后删除所有内容。所以只留下http://www.youtube.com/watch?v=6n8PGnc_cV4。
2。)我想将此网址转换为http://www.youtube.com/v/6n8PGnc_cV4
因为我不是一个正则表达式,所以我需要你的帮助:
$content = preg_replace('http://.*?\?v=[^&]*', '', $content);
return $content;
编辑:看看这个!我想创建一个非常简单的Wordpress插件,它只能识别我的$ content中的每个普通youtube网址,并用embedcode替换它:
<?php
function videoplayer($content) {
$embedcode = '<object class="video" width="308" height="100"><embed src="' . . '" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="308" height="100" wmode="opaque"></embed></object>';
//filter normal youtube url like http://www.youtube.com/watch?v=6n8PGnc_cV4&feature=rec-LGOUT-real_rn-2r-13-HM
//convert it to http://www.youtube.com/v/6n8PGnc_cV4
//use embedcode and pass along the new youtube url
$content = preg_replace('', '', $content);
//return embedcode
return $content;
}
add_filter('the_content', 'videoplayer');
?>
答案 0 :(得分:0)
我在我的脚本中使用此搜索条件:
/((http|ftp)\:\/\/)?([w]{3}\.)?(youtube\.)([a-z]{2,4})(\/watch\?v=)([a-zA-Z0-9_-]+)(\&feature=)?([a-zA-Z0-9_-]+)?/
答案 1 :(得分:0)
你可以将它拆分为第一个&符号。
$content = explode('&', $content);
$content = $content[0];
答案 2 :(得分:0)
编辑:最简单的正则表达式:/http:\/\/www\.youtube\.com\/watch\?v=.*/
Youtube链接都是一样的。要从它们获取视频ID,首先从末尾切掉额外的参数,然后切掉除最后11个字符之外的所有内容。看到它在行动:
$url = "http://www.youtube.com/watch?v=1rnfE4eo1bY&feature=...";
$url = $url.left(42); // "http://www.youtube.com/watch?v=1rnfE4eo1bY"
$url = $url.right(11); // "1rnfE4eo1bY"
$result = "http://www.youtube.com/v/" + $url; // "http://www.youtube.com/v/1rnfE4eo1bY"
您可以使用Greasemonkey脚本统一所有youtube链接(通过删除无用的参数):http://userscripts.org/scripts/show/86758。 Greasemonkey脚本本身作为Google Chrome中的插件支持。
作为奖励,这里有一个(好的,实际上是两个)班轮:
$url = "http://www.youtube.com/watch?v=1rnfE4eo1bY&feature=...";
$result = "http://www.youtube.com/v/" + $url.left(42).right(11);
- 3ICE
答案 3 :(得分:-1)
$url = "http://www.youtube.com/v/6n8PGnc_cV4";
$start = strpos($url,"v=");
echo 'http://www.youtube.com/v/'.substr($url,$start+2);