• <legend id='FBDPB'><style id='FBDPB'><dir id='FBDPB'><q id='FBDPB'></q></dir></style></legend>
    1. <small id='FBDPB'></small><noframes id='FBDPB'>

      • <bdo id='FBDPB'></bdo><ul id='FBDPB'></ul>
    2. <tfoot id='FBDPB'></tfoot>

        <i id='FBDPB'><tr id='FBDPB'><dt id='FBDPB'><q id='FBDPB'><span id='FBDPB'><b id='FBDPB'><form id='FBDPB'><ins id='FBDPB'></ins><ul id='FBDPB'></ul><sub id='FBDPB'></sub></form><legend id='FBDPB'></legend><bdo id='FBDPB'><pre id='FBDPB'><center id='FBDPB'></center></pre></bdo></b><th id='FBDPB'></th></span></q></dt></tr></i><div id='FBDPB'><tfoot id='FBDPB'></tfoot><dl id='FBDPB'><fieldset id='FBDPB'></fieldset></dl></div>
      1. Youtube I.D 解析新的 URL 格式

        时间:2023-10-11
        <legend id='RWGjg'><style id='RWGjg'><dir id='RWGjg'><q id='RWGjg'></q></dir></style></legend>

          <tfoot id='RWGjg'></tfoot>

            • <i id='RWGjg'><tr id='RWGjg'><dt id='RWGjg'><q id='RWGjg'><span id='RWGjg'><b id='RWGjg'><form id='RWGjg'><ins id='RWGjg'></ins><ul id='RWGjg'></ul><sub id='RWGjg'></sub></form><legend id='RWGjg'></legend><bdo id='RWGjg'><pre id='RWGjg'><center id='RWGjg'></center></pre></bdo></b><th id='RWGjg'></th></span></q></dt></tr></i><div id='RWGjg'><tfoot id='RWGjg'></tfoot><dl id='RWGjg'><fieldset id='RWGjg'></fieldset></dl></div>
                <tbody id='RWGjg'></tbody>
            • <small id='RWGjg'></small><noframes id='RWGjg'>

                  <bdo id='RWGjg'></bdo><ul id='RWGjg'></ul>
                  本文介绍了Youtube I.D 解析新的 URL 格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  以前有人问过这个问题,我发现了这个:

                  This question has been asked before and I found this:

                  YouTube 链接的注册 exp

                  但我正在寻找稍微不同的东西.

                  but I'm looking for something slightly different.

                  我需要匹配与所有可能的 youtube 链接格式兼容的 Youtube I.D 本身.不仅仅是从 youtube.com 开始.

                  I need to match the Youtube I.D itself compatible with all the possible youtube link formats. Not exclusively beginning with youtube.com.

                  例如:

                  http://www.youtube.com/watch?v=-wtIMTCHWuI

                  http://www.youtube.com/v/-wtIMTCHWuI?version=3&autohide=1

                  http://youtu.be/-wtIMTCHWuI

                  http://www.youtube.com/oembed?url=http%3A//www.youtube.com/watch?v%3D-wtIMTCHWuI&format=json

                  http://s.ytimg.com/yt/favicon-wtIMTCHWuI.ico

                  http://i2.ytimg.com/vi/-wtIMTCHWuI/hqdefault.jpg

                  是否有一种聪明的策略可以用来匹配与所有这些格式兼容的视频 I.D -wtIMTCHWuI.我在考虑字符计数和匹配 = ? / . & 字符.

                  is there a clever strategy I can use to match the video I.D -wtIMTCHWuI compatible with all these formats. I'm thinking character counting and matching = ? / . & characters.

                  推荐答案

                  我不得不在几周前写的一个 PHP 类中处理这个问题,最终得到一个匹配任何类型字符串的正则表达式:有或没有 URL方案,带或不带子域、youtube.com URL 字符串、youtu.be URL 字符串以及处理各种参数排序.您可以在 在 GitHub 上查看或者简单地复制并粘贴下面的代码块:

                  I had to deal with this for a PHP class I wrote a few weeks ago and ended up with a regex that matches any kind of strings: With or without URL scheme, with or without subdomain, youtube.com URL strings, youtu.be URL strings and dealing with all kind of parameter sorting. You can check it out at GitHub or simply copy and paste the code block below:

                  /**
                   *  Check if input string is a valid YouTube URL
                   *  and try to extract the YouTube Video ID from it.
                   *  @author  Stephan Schmitz <eyecatchup@gmail.com>
                   *  @param   $url   string   The string that shall be checked.
                   *  @return  mixed           Returns YouTube Video ID, or (boolean) false.
                   */
                  function parse_yturl($url)
                  {
                      $pattern = '#^(?:https?://|//)?(?:www.|m.)?(?:youtu.be/|youtube.com/(?:embed/|v/|watch?v=|watch?.+&v=))([w-]{11})(?![w-])#';
                      preg_match($pattern, $url, $matches);
                      return (isset($matches[1])) ? $matches[1] : false;
                  }
                  

                  测试用例:https://3v4l.org/GEDT0
                  JavaScript 版本:https://stackoverflow.com/a/10315969/624466

                  为了解释正则表达式,这里有一个拆分版本:

                  To explain the regex, here's a split up version:

                  /**
                   *  Check if input string is a valid YouTube URL
                   *  and try to extract the YouTube Video ID from it.
                   *  @author  Stephan Schmitz <eyecatchup@gmail.com>
                   *  @param   $url   string   The string that shall be checked.
                   *  @return  mixed           Returns YouTube Video ID, or (boolean) false.
                   */
                  function parse_yturl($url)
                  {
                      $pattern = '#^(?:https?://|//)?' # Optional URL scheme. Either http, or https, or protocol-relative.
                               . '(?:www.|m.)?'      #  Optional www or m subdomain.
                               . '(?:'                 #  Group host alternatives:
                               .   'youtu.be/'        #    Either youtu.be,
                               .   '|youtube.com/'    #    or youtube.com
                               .     '(?:'             #    Group path alternatives:
                               .       'embed/'        #      Either /embed/,
                               .       '|v/'           #      or /v/,
                               .       '|watch?v='    #      or /watch?v=,
                               .       '|watch?.+&v=' #      or /watch?other_param&v=
                               .     ')'               #    End path alternatives.
                               . ')'                   #  End host alternatives.
                               . '([w-]{11})'         # 11 characters (Length of Youtube video ids).
                               . '(?![w-])#';         # Rejects if overlong id.
                      preg_match($pattern, $url, $matches);
                      return (isset($matches[1])) ? $matches[1] : false;
                  }
                  

                  这篇关于Youtube I.D 解析新的 URL 格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何使用 PHP 检查 YouTube 上是否存在视频? 下一篇:正则表达式解析 youtube yid

                  相关文章

                    <i id='S3Uw8'><tr id='S3Uw8'><dt id='S3Uw8'><q id='S3Uw8'><span id='S3Uw8'><b id='S3Uw8'><form id='S3Uw8'><ins id='S3Uw8'></ins><ul id='S3Uw8'></ul><sub id='S3Uw8'></sub></form><legend id='S3Uw8'></legend><bdo id='S3Uw8'><pre id='S3Uw8'><center id='S3Uw8'></center></pre></bdo></b><th id='S3Uw8'></th></span></q></dt></tr></i><div id='S3Uw8'><tfoot id='S3Uw8'></tfoot><dl id='S3Uw8'><fieldset id='S3Uw8'></fieldset></dl></div>
                    <tfoot id='S3Uw8'></tfoot>
                        <bdo id='S3Uw8'></bdo><ul id='S3Uw8'></ul>

                    1. <legend id='S3Uw8'><style id='S3Uw8'><dir id='S3Uw8'><q id='S3Uw8'></q></dir></style></legend>

                      <small id='S3Uw8'></small><noframes id='S3Uw8'>