How do I use FILTER_VALIDATE_IP to handle an array?

0
 <input type="text" name="ip[]"/>

<?php
$ip = $_POST['ip'];

if(filter_var($ip, FILTER_VALIDATE_IP)) {
    echo("$ip is a valid IP address");
   }else {
echo("$ip is not a valid IP address");
}
?>

If I put any ip without being an array inside the variable that receives my ip and validates quietly, but when I pass an array, since the input field is dynamic, php does not accept the validation.

I've tried using FILTER_REQUIRE_ARRAY, but it did not work.

    
asked by anonymous 01.10.2018 / 20:01

1 answer

0

Well, in this case you will have an array as an answer, your $ ip variable will look like this:

array(
    0=>ip_1,
    1=>ip_2
    ...
)

To validate this, you will need to use a loop to loop through the keys and validate key by key.

It would be something like this:

foreach($ip = $i){
    if(filter_var($i, FILTER_VALIDATE_IP)) {
        echo("$i is a valid IP address");
    }else {
        echo("$i is not a valid IP address");
    }
}

See if it helps you there.

Hugs

    
01.10.2018 / 21:59