大家好,
想象一下这个URL:
http://www.youtube.com/watch?v=6n8PGnc_cV4&feature=rec-LGOUT-real_rn-2r-13-HM
什么是最干净和最好的正则表达式来执行以下操作:
1.)我想删除视频URL之后的所有内容。因此只有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 URL,并将其替换为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');
?>
最佳答案
我在脚本中使用以下搜索条件:
/((http|ftp)\:\/\/)?([w]{3}\.)?(youtube\.)([a-z]{2,4})(\/watch\?v=)([a-zA-Z0-9_-]+)(\&feature=)?([a-zA-Z0-9_-]+)?/
关于regex - 正则表达式查找youtube网址,剥离参数并返回干净的视频网址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4977233/