Home > database >  Validate text area with multiple Ip adresses, one per line
Validate text area with multiple Ip adresses, one per line

Time:06-30

i want to validate a Text area where the user can insert only ip adresses, one per line. This is my simple php code when I press the button:

$text_area = strip_tags($_POST["text_area"]);
$file = "ip_list.txt";
file_put_contents($file, $text_area);

This is the text area: Textarea

Thanks

CodePudding user response:

Try this code

$ips = explode("\n", str_replace("\r", "", $_POST["text_area"]));
foreach ($ips as $ip) {
    $isValidIp = filter_var($ip, FILTER_VALIDATE_IP);
    if ($isValidIp) {
        echo 'valid Ip';
    } else {
        echo 'invalid Ip';
    }
}

References:

TextArea to Array with PHP

How do I check if a users input is a valid IP address or not?

  •  Tags:  
  • php
  • Related