PHP-域验证用逗号分隔

时间:2013-09-15 15:23:21

标签: php

如何验证以逗号分隔的域名,例如来自textarea的site.com,site2.com,sub.site3.com 在PHP中返回true或alse。 感谢

if($_POST['domains'] != '')
{
//validation here
}
else
{
$check ="1";
    echo "Error, You didnt enter a domain name!";
}

1 个答案:

答案 0 :(得分:0)

使用this answer中的函数:

// returns true if $domain_name is valid; false otherwise
function is_valid_domain_name($domain_name) {
    return filter_var('abc@' . $domain_name, FILTER_VALIDATE_EMAIL) !== false;
}

// make sure $_POST['domains'] exists and isn't empty
if(isset($_POST['domains']) && $_POST['domains'] != '') {

    // split the domains around commas
    $domains = explode(',', $_POST['domains']);

    foreach($domains as $domain) {

        // remove white space from both ends of the string
        $domain = trim($domain);

        // validate the domain; if it fails, output an error but continue with the rest
        if(!is_valid_domain_name($domain)) {
            echo $domain, ' is not a valid domain name!', "\n";
        }
    }
}
else {
    $check = 1;
    echo 'Error, You didn\'t enter a domain name!';
}