Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php复杂验证逻辑_Php_Regex - Fatal编程技术网

Php复杂验证逻辑

Php复杂验证逻辑,php,regex,Php,Regex,我需要验证来自文本区域的输入。 这很复杂,我不知道如何才能做到最好? 你们能帮忙吗 文本区域的输入基本上是主机名或IP。输入可以是以下任意格式: x.x.x.x (single IP) x.x.x.x-x.x.x.x (range of IPs) x.x.x.x/x.x.x.x (IP and mask) x.x.x.x/xx (IP and CIDR) URL (with or without http:// and https:// prefixes) domain name in form

我需要验证来自文本区域的输入。 这很复杂,我不知道如何才能做到最好? 你们能帮忙吗

文本区域的输入基本上是主机名或IP。输入可以是以下任意格式:

x.x.x.x (single IP)
x.x.x.x-x.x.x.x (range of IPs)
x.x.x.x/x.x.x.x (IP and mask)
x.x.x.x/xx (IP and CIDR)
URL (with or without http:// and https:// prefixes)
domain name in format: xxxxxxx.xxx
还可以给出多个值,如: 192.168.1.1 192.168.1.2/192.168.1.4

我可以使用以下代码获取textbox的行:

$text = trim($targets);
$textAr = explode("\n", $text);
$textAr = array_filter($textAr, 'trim');

foreach ($textAr as $line) {


} 
我不能再继续下去了。请帮忙

谢谢,
Dave

如果您不介意稍微放松验证,您可以做一些简单的事情,例如:

function filter_fn($input)
{
    $input = trim($input);
    $regex_ip = '/^([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})$/';
    $regex_range = '/^([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})-([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})$/';
    $regex_cidr = '/^([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\/[0-9]{1,2})$/';
    $regex_sub = '/^([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})$/';

    if (filter_var($input, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => $regex_ip)))) {
        return $input;
    }

    if (preg_match($regex_range, $input)) {
        return $input;
    }

    if (preg_match($regex_cidr, $input)) {
         return $input;
    }

    if (preg_match($regex_sub, $input)) {
        return $input;
    }

    if (filter_var($input, FILTER_VALIDATE_URL)) {
        return $input;
    }

    if (filter_var('http://'.$input, FILTER_VALIDATE_URL)) {
        return $input;
    }

    return false;
}

$textAr = explode("\n", $text);
$textAr = array_filter($textAr, 'trim');
foreach ($textAr as $line) {
    $success = filter_var($line, FILTER_CALLBACK, array('options' => 'filter_fn'));
    if (!$success) {
        // It failed.
    } else {
        // It worked.
    }
} 
注意,在我的示例中,我将preg_match和filter_var与filter_VALIDATE_REGEXP一起使用。在这种情况下,两者都是相同的,因此第一个筛选器_var可以同样容易地替换为:

preg_match($regex_ip, $input)
或者,甚至:

filter_var($input, FILTER_VALIDATE_IP)

使用你也可以使用这个伟大的lib@sash为什么你删除了我的答案。我只是想帮忙谢谢你。当你说,‘如果我对验证稍有松懈’,你是什么意思?你能告诉我什么时候这不起作用吗?例如,如果你想验证ipv4地址是否确实有效,你还需要更多的验证。e、 g.这将通过555.555.555.555,因此需要额外的约束。这实际上取决于您的用例,这是否可行。现在,它非常基本,可以用于简单的验证。