$str1 = "cat dog mouse";
$ipnum = array("127.0.0.1",
"255.255.255.255",
"204.28.82.290",
"209.192.28.28.34");
// preg() is case sensitive so CAT will not be found
if( ("CAT", $str1) )
echo('CAT found.
');
else
echo('CAT not found.
');
// pregi() is case insensitive so dog is found
if( pregi("DOG", $str1) )
echo('dog found.
');
else
echo('dog not found.
');
// Replace 'mouse' with 'cat'
if( $str1 = preg_replace("mouse", "CAT", $str1) )
echo('mouse replaced with cat
');
else
echo('mouse not found.
');
echo('$str1 now equals "' . $str1 . '"
');
// Case insensitive replace
if( $str1 = pregi_replace("cat", "mouse", $str1) )
echo('cat and CAT replace with mouse
');
else
echo('no match for cat
');
echo('$str1 now equals "' . $str1 . '"
');
// Cycle through the ip array and test each one
foreach ($ipnum as $ipn) {
if(valid_ip($ipn))
echo("$ipn is a valid ip.
");
else
echo("$ipn is an invalid ip.
");
}
// Takes an ip as an arg and tests to see if it is valid
function valid_ip($ip) {
if( preg( "^([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3})$", $ip, $res ) ) {
for($x = 1; $x < 5; $x++)
if($res[$x] > 255)
return FALSE;
} else {
return FALSE;
}
return TRUE;
}
?>