<tfoot id='tYlHf'></tfoot>
  • <small id='tYlHf'></small><noframes id='tYlHf'>

          <bdo id='tYlHf'></bdo><ul id='tYlHf'></ul>
        <legend id='tYlHf'><style id='tYlHf'><dir id='tYlHf'><q id='tYlHf'></q></dir></style></legend>

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

        转到 PHP parse_url() 没有的地方 - 仅解析域

        Going where PHP parse_url() doesn#39;t - Parsing only the domain(转到 PHP parse_url() 没有的地方 - 仅解析域)

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

      2. <tfoot id='nysxh'></tfoot>
      3. <legend id='nysxh'><style id='nysxh'><dir id='nysxh'><q id='nysxh'></q></dir></style></legend>

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

                  <i id='nysxh'><tr id='nysxh'><dt id='nysxh'><q id='nysxh'><span id='nysxh'><b id='nysxh'><form id='nysxh'><ins id='nysxh'></ins><ul id='nysxh'></ul><sub id='nysxh'></sub></form><legend id='nysxh'></legend><bdo id='nysxh'><pre id='nysxh'><center id='nysxh'></center></pre></bdo></b><th id='nysxh'></th></span></q></dt></tr></i><div id='nysxh'><tfoot id='nysxh'></tfoot><dl id='nysxh'><fieldset id='nysxh'></fieldset></dl></div>
                    <tbody id='nysxh'></tbody>
                  本文介绍了转到 PHP parse_url() 没有的地方 - 仅解析域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  PHP 的 parse_url() 有一个主机字段,其中包括完整的主机.我正在寻找仅返回域和 TLD 的最可靠(且成本最低)的方法.

                  PHP's parse_url() has a host field, which includes the full host. I'm looking for the most reliable (and least costly) way to only return the domain and TLD.

                  举个例子:

                  • http://www.google.com/foo,parse_url() 返回 www.google.com为主机
                  • http://www.google.co.uk/foo,parse_url() 返回www.google.co.uk 主机
                  • http://www.google.com/foo, parse_url() returns www.google.com for host
                  • http://www.google.co.uk/foo, parse_url() returns www.google.co.uk for host

                  我只查找 google.comgoogle.co.uk.我已经考虑了一个有效 TLD/后缀的表格,并且只允许这些和一个词.你会用其他方式吗?有没有人知道这种事情的预装有效正则表达式?

                  I am looking for only google.com or google.co.uk. I have contemplated a table of valid TLD's/suffixes and only allowing those and one word. Would you do it any other way? Does anyone know of a pre-canned valid REGEX for this sort of thing?

                  推荐答案

                  这样的事情怎么样?

                  function getDomain($url) {
                    $pieces = parse_url($url);
                    $domain = isset($pieces['host']) ? $pieces['host'] : '';
                    if (preg_match('/(?P<domain>[a-z0-9][a-z0-9-]{1,63}.[a-z.]{2,6})$/i', $domain, $regs)) {
                      return $regs['domain'];
                    }
                    return false;
                  }
                  

                  将使用经典的 parse_url 提取域名,然后查找没有任何子域的有效域(www 是子域).不适用于本地主机"之类的东西.如果没有匹配任何内容,将返回 false.

                  Will extract the domain name using the classic parse_url and then look for a valid domain without any subdomain (www being a subdomain). Won't work on things like 'localhost'. Will return false if it didn't match anything.

                  //

                  试试看:

                  echo getDomain('http://www.google.com/test.html') . '<br/>';
                  echo getDomain('https://news.google.co.uk/?id=12345') . '<br/>';
                  echo getDomain('http://my.subdomain.google.com/directory1/page.php?id=abc') . '<br/>';
                  echo getDomain('https://testing.multiple.subdomain.google.co.uk/') . '<br/>';
                  echo getDomain('http://nothingelsethan.com') . '<br/>';
                  

                  它应该返回:

                  google.com
                  google.co.uk
                  google.com
                  google.co.uk
                  nothingelsethan.com
                  

                  当然,如果没有通过parse_urla>,因此请确保它是格式正确的 URL.

                  Of course, it won't return anything if it doesn't get through parse_url, so make sure it's a well-formed URL.

                  //附录:

                  Alnitak 是对的.上述解决方案适用于大多数情况,但不一定适用于所有情况,并且需要维护以确保它们不是带有 .morethan6characters 等的新 TLD.提取域的唯一可靠方法是使用维护列表,例如 http://publicsuffix.org/.一开始会更痛苦,但从长远来看会更容易、更稳健.您需要确保了解每种方法的优缺点以及它如何适合您的项目.

                  Alnitak is right. The solution presented above will work in most cases but not necessarily all and needs to be maintained to make sure, for example, that their aren't new TLD with .morethan6characters and so on. The only reliable way of extracting the domain is to use a maintained list such as http://publicsuffix.org/. It's more painful at first but easier and more robust on the long-term. You need to make sure you understand the pros and cons of each method and how it fits with your project.

                  这篇关于转到 PHP parse_url() 没有的地方 - 仅解析域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                    <tbody id='Lzj05'></tbody>
                      <bdo id='Lzj05'></bdo><ul id='Lzj05'></ul>

                      <tfoot id='Lzj05'></tfoot>

                          <legend id='Lzj05'><style id='Lzj05'><dir id='Lzj05'><q id='Lzj05'></q></dir></style></legend>

                          • <small id='Lzj05'></small><noframes id='Lzj05'>

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