Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.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 验证不带筛选器的URL\u验证\u URL_Php - Fatal编程技术网

Php 验证不带筛选器的URL\u验证\u URL

Php 验证不带筛选器的URL\u验证\u URL,php,Php,我在玩弄验证,所有的一切都在膨胀,除了我不太喜欢FILTER\u VALIDATE\u URL PHP过滤器,或者我没有正确使用它。这种类型的输入将验证: www.mysite notice no.com 我希望这样做能够奏效: www.mysite.com mysite.com 这是我现在使用的代码 if (empty($web)) { $webError = '<p class="error">Website Is Required</p>'; } else if

我在玩弄验证,所有的一切都在膨胀,除了我不太喜欢FILTER\u VALIDATE\u URL PHP过滤器,或者我没有正确使用它。这种类型的输入将验证:

www.mysite notice no.com

我希望这样做能够奏效:

www.mysite.com

mysite.com

这是我现在使用的代码

if (empty($web)) {
$webError = '<p class="error">Website Is Required</p>';
}

else if (filter_var($web, FILTER_VALIDATE_URL) === FALSE) {
$webError = '<p class="error">Please Enter A Valid URL</p>';
}

当然,只会显示一条错误消息,因为您每次都会重写该字符串。而你完全错了。您需要某种错误容器来存储它们。然后,如果它有错误,在HTML标记中显示它们。你的代码可能是这样的

$errors = array();

if (empty($web)) { 
   array_push($errors, 'Website Is Required');

} elseif (filter_var($web, FILTER_VALIDATE_URL) === false) {

   array_push($errors, 'Please Enter A Valid URL');
}

?>

<?php if (!empty($errors)) : ?>
<?php foreach($errors as $error): ?>

<p class="error"><?php echo $error; ?></p>

<?php endforeach; ?>
<?php endif; ?>

看起来不错,我把它捣碎后再给你回复。不过我已经喜欢了…因为你说的对,我正在接近一堆单独的$error变量,而不是在数组中进行操作。。