filter_var() 함수 사용으로 간단하게 가능.

<?php

$a = [
    '0.0.0.0',
    '127.0.0.1',
    '192.168.0.1',
    '172.16.0.1',
    '255.255.255.0',
    '8.8.8.8'
];

foreach ($a as $ip) {
    $valid = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) === false ? 'X' : 'O';

    printf("%s = %s\n", $ip, $valid);
}

// 출력
0.0.0.0       = X  
127.0.0.1     = X  
192.168.0.1   = X  
172.16.0.1    = X  
255.255.255.0 = X  
8.8.8.8       = O

 

 

[참고]

https://www.php.net/filter_var

https://www.php.net/manual/en/filter.filters.flags.php

Posted by bloodguy

댓글을 달아 주세요