【问题标题】:simple regular expression for vimeo videosvimeo 视频的简单正则表达式
【发布时间】:2011-03-15 19:34:04
【问题描述】:

我将如何编写一个正则表达式来检查两者是否匹配?

http://vimeo.com/moogaloop.swf?clip_id=CLIP_ID

http://player.vimeo.com/video/CLIP_ID

【问题讨论】:

  • 抓取是邪恶的。收集您自己的内容。
  • 写 2 个表达式怎么样,每个表达式一个?另外,你写过什么吗?如果你写了一个,那么更正你的 RE 会更容易。
  • @Tomalak - 谁说他在刮?也许他想写一个检测 vimeo 链接的 CMS?
  • 是的,我有这个:%vimeo\.com/(\d{7,9})\b% 我知道它只查找 vimeo.com/video_id
  • 是的,我实际上只是在我们网站的博客文章中搜索视频站点地图

标签: php regex vimeo


【解决方案1】:

此正则表达式适用于两者(已测试):

preg_match('#http://(\w+.)?vimeo.com/(video/|moogaloop\.swf\?clip_id=)\w+#i', $content);

更新

要捕获clip_id,请使用此调整后的正则表达式:

preg_match('#http://(?:\w+.)?vimeo.com/(?:video/|moogaloop\.swf\?clip_id=)(\w+)#i', $content, $match);

$match[1] 包含剪辑 ID

基本上在每个 '()' 中添加 '?:' 会告诉它不显示在 $match 数组中。

【讨论】:

  • 这个有效,但匹配是这样的: Array ( [0] => Array ( [0] => vimeo.com/moogaloop.swf?clip_id=1018674 [1] => vimeo.com/moogaloop.swf?clip_id=1018674 ) [1] => Array ( [0] => www. [1] => www.) [2] => 数组 ( [0] => moogaloop.swf?clip_id= [1] => moogaloop.swf?clip_id= ) )
  • 有什么方法可以捕获clip_id?
  • 我的荣幸。很高兴我能帮上忙。
  • 您的正则表达式的更新版本不适用于此 URL vimeo.com/29950141 :(
  • @juanchopx2 对于该网址,请尝试: preg_match('#http://(?:\w+.)?vimeo.com/(\d+)#i', $content, $match);同样 $match[1] 是您来自 url 的 id。注意这个正则表达式的 id 必须是数字。
【解决方案2】:
function getVimeoInfo($link)
 {
    if (preg_match('~^http://(?:www\.)?vimeo\.com/(?:clip:)?(\d+)~', $link, $match)) 
    {
        $id = $match[1];
    }
    else
    {
        $id = substr($link,10,strlen($link));
    }

    if (!function_exists('curl_init')) die('CURL is not installed!');
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://vimeo.com/api/v2/video/$id.php");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    $output = unserialize(curl_exec($ch));
    $output = $output[0];
    curl_close($ch);
    return $output;
}

【讨论】:

    【解决方案3】:

    未经测试但应该可以工作:

    preg_match( '/http:\/\/(?:player\.)?vimeo\.com\/(?:moogaloop\.swf\?clip_id=|video\/)/',$string)
    

    $string 是你匹配的对象。

    【讨论】:

      猜你喜欢
      • 2011-06-27
      • 1970-01-01
      • 1970-01-01
      • 2012-06-01
      • 2012-03-22
      • 2023-03-17
      • 1970-01-01
      • 2016-01-17
      • 2021-04-30
      相关资源
      最近更新 更多