【问题标题】:Regexp to find youtube url, strip off parameters and return clean video url?正则表达式查找 youtube url,剥离参数并返回干净的视频 url?
【发布时间】: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');  
?>

【问题讨论】:

    标签: regex youtube


    【解决方案1】:

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

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

    【讨论】:

    • 那么我如何在上面的函数中使用它来获得我想要的东西?我需要去掉所有附加参数并将 /watch?v= 转换为 /v/
    【解决方案2】:

    你可以在第一个 & 号上拆分它。

    $content = explode('&', $content);
    $content = $content[0];
    

    【讨论】:

    • 唯一的问题是参数是否乱序。例如。 youtube.com/watch?feature=player_embedded&v=EhiMrEvfvo4.
    • Youtube url 的顺序很规律,看看它们通常是如何由网站上的一个生成器生成的。
    • 但是,在我输入这个答案后查看 OP 所做的编辑,很明显他正在寻找不同的东西......
    【解决方案3】:

    编辑:最简单的正则表达式:/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

    【讨论】:

    • 甚至没有关闭。 YouTube 链接有多种格式:stackoverflow.com/questions/5830387/…
    • 那时候。自 2010 年以来,所有链接都符合以下两个模板之一:http://www.youtube.com/watch?v=* 或 https://www。 youtube.com/watch?v=* 您链接中的示例:youtu.be 短格式现在始终扩展为完整 URL。用户和频道页面链接不再用于观看视频。 (反正我从来不喜欢这些链接。)有些重定向到正确的 URL,有些只是显示上传链接视频的人的频道。附加参数现在总是出现在初始 v 参数之后。可能没有人使用 nocookie 域,它不再起作用了。
    【解决方案4】:
    $url = "http://www.youtube.com/v/6n8PGnc_cV4";
    $start = strpos($url,"v=");
    echo 'http://www.youtube.com/v/'.substr($url,$start+2);
    

    【讨论】:

    • 我没有这样的 $url!我需要一个正则表达式来过滤任何给定 youtubeurl 的 $content。然后将其转换为此 /v/ url 并将其返回到嵌入代码中。看看我的编辑!
    • 所以它是一个 WP 插件? wordpress.org/extend/plugins/smart-youtubewordpress.org/extend/plugins/wp-youtube 有什么问题?
    • 问题是我想构建自己的,以便使用我自己的嵌入代码。我想这不应该那么难。这只是 1.) 检查字符串是否与 youtube 模式匹配,2.) 在嵌入代码中返回它。
    猜你喜欢
    • 1970-01-01
    • 2012-12-24
    • 2011-06-10
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-23
    相关资源
    最近更新 更多