• <bdo id='L4wkx'></bdo><ul id='L4wkx'></ul>

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

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

        <legend id='L4wkx'><style id='L4wkx'><dir id='L4wkx'><q id='L4wkx'></q></dir></style></legend>
      3. Preg_match:在一个表达式中从 url 中获取最后两个域段

        时间:2023-09-23

          <legend id='yQXTr'><style id='yQXTr'><dir id='yQXTr'><q id='yQXTr'></q></dir></style></legend>
              <tbody id='yQXTr'></tbody>

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

                <bdo id='yQXTr'></bdo><ul id='yQXTr'></ul>

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

                  本文介绍了Preg_match:在一个表达式中从 url 中获取最后两个域段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  php.net 上有一个例子,如何分两步获取最后两个域段:

                  There is an example on php.net how to get last two domain segments in two steps:

                      <?php 
                   //get host name from URL
                  preg_match("/^(http://)?([^/]+)/i",
                      "http://www.php.net/index.html", $matches);
                  $host = $matches[2];
                  
                  // get last two segments of host name
                  preg_match("/[^./]+.[^./]+$/", $host, $matches);
                  echo "domain name is: {$matches[0]}
                  ";
                  
                  /* Output is php.net */
                  
                  ?>
                  

                  但是我怎样才能一步完成,只使用一个 preg_match 表达式?

                  But how can I do it in one step, using only one preg_match expression?

                  推荐答案

                  这段代码:

                  $domain = 'http://www.php.net/index.html';
                  $url    = parse_url($domain);
                  $tokens = explode('.', $url['host']);
                  
                  print_r($tokens);
                  

                  会给你这个数据:

                  Array
                  (
                      [0] => www
                      [1] => php
                      [2] => net
                  )
                  

                  我相信不需要正则表达式,因为很难用它们正确解析 URL.从生成的 $tokens 数组中,您可以轻松提取主机名的任何部分.

                  I believe there is no need for regexs as far as it's very hard to properly parse URL with them. From resulting $tokens array you can extract any part of host name easily.

                  更新:

                  print_r($url);
                  

                  $url 数组包含所有必要的详细信息:

                  $url array contains all necessary details:

                  Array
                  (
                      [scheme] => http
                      [host] => www.php.net
                      [path] => /index.html
                  )
                  

                  这篇关于Preg_match:在一个表达式中从 url 中获取最后两个域段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:仅在使用 DNS 时 AJAX 请求上的 405(不允许的方法) 下一篇:在没有 Git 的情况下读取 Git 存储库

                  相关文章

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

                      <legend id='XGrd4'><style id='XGrd4'><dir id='XGrd4'><q id='XGrd4'></q></dir></style></legend>
                    1. <tfoot id='XGrd4'></tfoot>
                      • <bdo id='XGrd4'></bdo><ul id='XGrd4'></ul>

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