I did not give the code a close inspection, but an idea occurs to me: you could just use the [man]ip2long/man function to validate it:
if(ip2long($ip_address) === false) { // it's invalid }
Or if using PHP 5.2.0+ :
if(filter_var($ip_address, FILTER_VALIDATE_IP) === false) { // it's invalid }