If the input string is not a readable IP address, inet_pton() generates an E_WARNING and returns FALSE. The same is true for inet_ntop().
Also, inet_pton() does not recognize netmask notation (e.g: "1.2.3.4/24" or "1:2::3:4/64") in the input string. This differs from how some database systems (like postgreSQL) support IP address types, so if you need that sort of functionality when processing IP addresses in PHP you'll have to write it in yourself.
A rough example:
<?php
$ipaddr = '1.2.3.4/24'; $ipaddr = '1:2::3:4/64'; $cx = strpos($ipaddr, '/');
if ($cx)
{
$subnet = (int)(substr($ipaddr, $cx+1));
$ipaddr = substr($ipaddr, 0, $cx);
}
else $subnet = null; $addr = inet_pton($ipaddr);
foreach(str_split($addr) as $char) echo str_pad(dechex(ord($char)), 2, '0', STR_PAD_LEFT);
echo "<br />\n";
if (is_integer($subnet))
{
$len = 8*strlen($addr);
if ($subnet > $len) $subnet = $len;
$mask = str_repeat('f', $subnet>>2);
switch($subnet & 3)
{
case 3: $mask .= 'e'; break;
case 2: $mask .= 'c'; break;
case 1: $mask .= '8'; break;
}
$mask = str_pad($mask, $len>>2, '0');
$mask = pack('H*', $mask);
}
foreach(str_split($mask) as $char) echo str_pad(dechex(ord($char)), 2, '0', STR_PAD_LEFT);
?>