【发布时间】:2011-02-12 08:59:05
【问题描述】:
想象一下这个网址:
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 并将其替换为嵌入代码:
<?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');
?>
【问题讨论】: