【问题标题】:Regex to find Youtube Link in string [duplicate]正则表达式在字符串中查找 Youtube 链接 [重复]
【发布时间】:2015-03-14 04:17:04
【问题描述】:

我有一个这样的字符串:

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard  dummy text ever since the 1500s, https://www.youtube.com/watch?v=7TL02DA5MZM when an unknown printer took a galley of type and scrambled it to make a type

这就是我所拥有的:

preg_match("(?:http://)?(?:www.)?(?:youtube.com|youtu.be)/(?:watch\?)?([^\s]+?)", $content, $m);
    var_dump( $m );

并希望从中提取 youtube 链接。 视频ID也可以。

不胜感激!

【问题讨论】:

    标签: php regex preg-match


    【解决方案1】:

    这对你有用,

    \S*\bwww\.youtube\.com\S*
    

    \S* 匹配零个或多个非空格字符。

    代码是,

    preg_match('~\S*\bwww\.youtube\.com\S*~', $str, $matches);
    

    DEMO

    我对你原来的正则表达式做了一些更正。

    (?:https?://)?(?:www.)?(?:youtube.com|youtu.be)/(?:watch\?v=)?([^\s]+)
    

    DEMO

    $str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard  dummy text ever since the 1500s, https://www.youtube.com/watch?v=7TL02DA5MZM when an unknown printer took a galley of type and scrambled it to make a type";
    preg_match('~(?:https?://)?(?:www.)?(?:youtube.com|youtu.be)/(?:watch\?v=)?([^\s]+)~', $str, $match);
    print_r($match);
    

    输出:

    Array
    (
        [0] => https://www.youtube.com/watch?v=7TL02DA5MZM
        [1] => 7TL02DA5MZM
    )
    

    【讨论】:

    • 我的正则表达式出错:未知修饰符“?”在
    • 查看我的更新。它对我有用。我认为您忘记添加分隔符。
    • 它忽略了 youtu.be (ex.youtu.be/NFD1dtTosQc)
    • 查看我的更新.. 只需将v= 放到上一个可选组中即可。
    • 不要忘记转义正斜杠字符!
    【解决方案2】:
    (?:https?:\/\/)?www\.youtube\.com\S+?v=\K\S+
    

    您可以通过匹配 youtube url 然后使用 \K 丢弃来获取视频 ID。参见演示。

    https://regex101.com/r/tX2bH4/21

    $re = "/(?:https?:\\/\\/)?www\\.youtube\\.com\\S+?v=\\K\\S+/i";
    $str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, https://www.youtube.com/watch?v=7TL02DA5MZM when an unknown printer took a galley of type and scrambled it to make a type";
    
    preg_match_all($re, $str, $matches);
    

    【讨论】:

    • 几乎没问题。那么youtu.be呢?
    • @user998163 (?:youtube\.com|youtu\.be) 在 YouTube\.com 中使用它
    • 你能更新你的回放吗?我试过 (?:youtube\.com|youtu\.be) 但没有成功。
    【解决方案3】:

    我想出了以下正则表达式:

    https?:\/\/(w{3}\.)?youtube\.com\/watch\?.+?(\s|$)
    

    这是我的使用方法:

    $str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard  dummy text ever since the 1500s, https://www.youtube.com/watch?v=7TL02DA5MZM when an unknown printer took a galley of type and scrambled it to make a type";
    
    preg_match("/https?:\/\/(w{3}\.)?youtube\.com\/watch\?.+?(\s|$)/", $str, $matches);
    
    $ytube = $matches[0];
    $parse = parse_url($ytube);
    parse_str($parse["query"], $query);
    
    echo $ytube;
    print_r($parse);
    print_r($query);
    

    这是项目的输出:

    https://www.youtube.com/watch?v=7TL02DA5MZM
    Array
    (
        [scheme] => https
        [host] => www.youtube.com
        [path] => /watch
        [query] => v=7TL02DA5MZM 
    )
    Array
    (
        [v] => 7TL02DA5MZM 
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多