【问题标题】:PHP REGEX: Get domain from URLPHP 正则表达式:从 URL 获取域
【发布时间】:2011-03-27 10:05:06
【问题描述】:

我想要什么


我想从URL 得到domain 部分所以从http://example.com/ -> example.com

示例:


+----------------------------------------------+-----------------------+
| input                                        | output                |
+----------------------------------------------+-----------------------+
| http://www.stackoverflow.com/questions/ask   | www.stackoverflow.com |
| http://validator.w3.org/check                | validator.w3.org      |
| http://www.google.com/?q=hello               | www.google.com        |
| http://google.de/?q=hello                    | google.de             |
+----------------------------------------------+-----------------------+

我在stackoverflow 中发现了一些相关问题,但没有一个是我想要的。

感谢您的帮助!

【问题讨论】:

标签: php regex url dns


【解决方案1】:

没有必要为此使用正则表达式。 PHP 有一个内置函数可以做到这一点。使用parse_url():

$domain = parse_url($url, PHP_URL_HOST);

【讨论】:

  • 只有在包含 http(s) 时才有效,而不适用于“stackoverflow.com/questions”
  • 这也会给你子域。小心,因为parse_url('http://example.com', PHP_URL_HOST) == parse_url('http://www.example.com', PHP_URL_HOST) 将返回 false
【解决方案2】:

我用:

$domain = parse_url('http://' . str_replace(array('https://', 'http://'), '', $url), PHP_URL_HOST);

因为$url 中缺少架构时parse_url 不返回主机密钥。

【讨论】:

    【解决方案3】:

    这类似于 regex from theraccoonbear,但支持 HTTPS 域。

    if (preg_match('/https?:\/\/([^\/]+)\//i', $target_string, $matches)) {
      $domain = $matches[1];
    }
    

    【讨论】:

      【解决方案4】:

      假设http:// 是所有内容的前缀。

      $tmp = explode("/", $url);
      $domain = $tmp[2];
      

      【讨论】:

      • 嘿,最开箱即用的解决方案 :-)
      【解决方案5】:
      $tmp = parse_url($url);
      $url = $tmp['host']
      

      【讨论】:

        【解决方案6】:

        我认为以下正则表达式可能会回答您的问题。

        This diagram 解释了它是如何工作的,或者更确切地说,它为什么工作:-)

        $regexp = '/.*\/\/([^\/:]+).*/';
        
        // www.stackoverflow.com
        echo preg_replace($regexp, '$1', 'http://www.stackoverflow.com/questions/ask');
        
        // google.de
        echo preg_replace($regexp, '$1', 'http://google.de/?q=hello');
        
        // it works for the other input tests too ;-)
        

        【讨论】:

          【解决方案7】:

          这是我快速而肮脏的解决方案。

          http://([^/]+).*

          我还没有测试过,但它应该可以抓取http:// 和第一个斜杠之间的任何内容。

          【讨论】:

            【解决方案8】:
            if (preg_match('/http:\/\/([^\/]+)\//i', $target_string, $matches)) {
              $domain = $matches[1];
            }
            

            【讨论】:

              【解决方案9】:

              我认为最好的方法:

              preg_match('/(http(|s)):\/\/(.*?)\//si',  'http://www.example.com/page/?bla=123#!@#$%^&*()_+', $output);
              // $output[0] ------------>  https://www.example.com/
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2021-06-05
                • 2011-10-12
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多