php如何检查变量字符串?
我想检查标准输入中的几个IP地址。 我一次又一次地犯错误……php如何检查变量字符串?,php,Php,我想检查标准输入中的几个IP地址。 我一次又一次地犯错误…… preg_match_all()希望参数2是字符串,数组在 /第14行的workspace/Main.php 我尝试输入字符串,但它重新确认标准输入只是其中一部分。。。所以结果总是错误的。。。。 如何让它工作 我想要的结果是 假的 真的 假的 真的 现在我有了这个代码。 标准输入: 4 192.400.1.10.1000... 4.3.2.1 0..33.444... 1.2.3.4 我的代码: <?php $input =
preg_match_all()希望参数2是字符串,数组在 /第14行的workspace/Main.php
我尝试输入字符串,但它重新确认标准输入只是其中一部分。。。所以结果总是错误的。。。。 如何让它工作 我想要的结果是 假的 真的 假的 真的 现在我有了这个代码。
标准输入:
4
192.400.1.10.1000...
4.3.2.1
0..33.444...
1.2.3.4
我的代码:
<?php
$input = trim(fgets(STDIN));
while ($input){
$array[] = $input;
$input = trim(fgets(STDIN));
}
$test = checkIPAddr($array);
print_r($array);
function checkIPAddr($ip){
$result = "False";
if (preg_match_all('/^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$/',$ip)){
$result = "True";
}
return $result;
}
echo $test;
?>
preg\u match\u all中的第二个参数是数组。它只能接受字符串
if (preg_match_all('/^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$/',$ip)){
$ip是一个数组。请尝试对代码进行小修改:
<?php
$input = trim(fgets(STDIN));
while ($input){
$array[] = $input;
$input = trim(fgets(STDIN));
}
if(checkIPAddr($array)){ echo "True";}
else {echo "False";}
function checkIPAddr($ip){
if (preg_match_all('/^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$/',$ip)){
return true;
}
else {return false;}
}
?>
这也提出了解决方案,通过数组元素在循环中调用函数。您还应该在答案中添加一个。preg_match_all()接受参数2作为字符串,您应该传递匹配字符串/变量以与$ip匹配。您缺少匹配字符串/变量您更改了什么,如何使其更好,这是否解决了问题?Explain.preg_match_all()接受参数2作为字符串,您应该传递匹配字符串/变量以与$ip匹配。您缺少匹配字符串/变量,但仍然为preg\u match\u all()
提供了一个数组<代码>$array[]=$input代码>它仍然不是字符串。反正已经关门了。