本文介绍了使用正则表达式查找 youtube url、删除参数并返回干净的视频 url?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,想象一下这个网址:

hey guys,imagine this url:

http://www.youtube.com/watch?v=6n8PGnc_cV4&feature=rec-LGOUT-real_rn-2r-13-HM

执行以下操作的最干净和最好的正则表达式是什么:

what is the cleanest and best regexp to do the following:

1.) 我想去掉视频 URL 之后的所有内容.这样就只剩下 http://www.youtube.com/watch?v=6n8PGnc_cV4.

1.) i want to strip off every thing after the video URL. so that only http://www.youtube.com/watch?v=6n8PGnc_cV4 remains.

2.) 我想将此网址转换为 http://www.youtube.com/v/6n8PGnc_cV4

2.) i want to convert this url into http://www.youtube.com/v/6n8PGnc_cV4

因为我不是一个正则表达式专家,所以我需要你的帮助:

Since i'm not much of a regexp-ert i need your help:

$content = preg_replace('http://.*?\?v=[^&]*', '', $content); 

return $content;

看看这个!我想创建一个非常简单的 Wordpress 插件,它可以识别 $content 中的每个普通 youtube URL 并将其替换为嵌入代码:

edit: check this out! I want to create the a really simpel Wordpress plugin that just recognizes every normal youtube URL in my $content and replaces it with the 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');  
?>

推荐答案

我在我的脚本中使用这个搜索条件:

I use this search criteria in my script:

/((http|ftp)\:\/\/)?([w]{3}\.)?(youtube\.)([a-z]{2,4})(\/watch\?v=)([a-zA-Z0-9_-]+)(\&feature=)?([a-zA-Z0-9_-]+)?/

这篇关于使用正则表达式查找 youtube url、删除参数并返回干净的视频 url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 06:10