function getexpr_callback($m)
{
$s = min($m[1],$m[2]);
$e = max($m[1],$m[2]);
$a = array();
for ($i = $s; $i <= $e; $i++)
{
$a[] = $i;
}
return '(?:'.implode('|',$a).')';
}
function getexpr($mask)
{
$e = explode('.',$mask);
for ($i = 0,$s = sizeof($e); $i < $s; $i++)
{
$x = explode(',',$e[$i]);
for ($j = 0,$d = sizeof($x); $j < $d; $j++)
{
$x[$j] = preg_replace_callback('~(\d+)\-(\d+)~','getexpr_callback',$x[$j]);
}
if (sizeof($x) > 1) {$e[$i] = '(?:'.implode('|',$x).')';}
else {$e[$i] = $x[0];}
}
return implode('\.',$e);
}
echo getexpr('192.168.1.1-40');