【问题标题】:How to validate domain name without http or https in PHP? [duplicate]如何在 PHP 中验证没有 http 或 https 的域名? [复制]
【发布时间】:2013-10-01 04:22:03
【问题描述】:

我需要从文本字符串中验证域名以处理以下内容:

www.dewalist.com
dewaxxxx.com
ng.dewalist.com
dewaster.com.au
test.co.uk
...
...

我可以使用 FILTER_VALIDATE_URL 但这只会验证完整的 url:http://www.dewalist.com bla bla,这并不是我真正想要的。

类似的功能,但不是用于电子邮件,而是用于域:

function extract_email_address ($string) 
    {
       $emails = array();
       $string = str_replace("\r\n",' ',$string);
       $string = str_replace("\n",' ',$string);

       foreach(preg_split('/ /', $string) as $token) {
            $email = filter_var($token, FILTER_VALIDATE_EMAIL);
            if ($email !== false) { 
                $emails[] = $email;
            }
        }
        return $emails;
    }

【问题讨论】:

标签: php


【解决方案1】:

您可以使用parse_url() 将 URL 拆分为逻辑部分,而不是通过正则表达式仅验证您需要的部分。

http://php.net/parse_url

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-17
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 2013-08-22
    • 1970-01-01
    • 2015-06-18
    相关资源
    最近更新 更多