Skip to content
Advertisement

Validate text area with multiple Ip adresses, one per line

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

Advertisement

Answer

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?

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement