-1

我如何验证以逗号分隔的域,例如来自 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!";
}
4

1 回答 1

0

使用此答案中的函数:

// 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!';
}
于 2013-09-15T15:29:53.733 回答