Regular expression to match both domain and IP Address

I want to check a form data if its a domain name or ipaddress.

I am using this code . $link contains the data from form

<?php
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
$reg_exip = "/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/";
if( preg_match($reg_exUrl, $link) || preg_match($reg_exip, $link)) {
echo "Link is ok";
} else {
 echo "It didnt match";
} 
}
?>

i want these two example to be accepted

http://exp.com

http://192.168.1.1

Do you mean the domain name or IP of the person on your site? I use IP all the time. It is simple. If you know the good
IP address is, let’s say 192.168.1.1 , then you can check for that like this in PHP:

$ipaddress = getenv(‘REMOTE_ADDR’);
if ($ipaddress!=“192.168.1.1”) {
header(“NoAccess.html”); // Redirect the user to a page saying he/her not allowed here!
}

Is that what you need?

Sponsor our Newsletter | Privacy Policy | Terms of Service