简体   繁体   中英

How to check if a variable contains a valid url using PHP and jQuery?

I'm trying to make a whois check script.

User can submit some domain address and then get a message if it's available or not.

$_POST['url'] is the submitted value by user.

How do I know if this variable is a domain name address?

It should give true for domains like:

http://google.com
www.google.com
http://www.google.com
google.com

Same for javascript (I'm using ajax-validation also)?

如果要检查该URL是否为有效URL,可以将filter_var()FILTER_VALIDATE_URL过滤器一起使用。

filter_var($_POST['url'], FILTER_VALIDATE_URL)

You can use the following code:

Example:

$url = "http://0gate.com"; // you can use instead - $_POST['url']
if (!preg_match("/^[http|https]*[:\/\/]*[A-Za-z0-9\-_]+\.([A-Za-z]{3,4})+([\.A-Za-z]{3})*$/i", $url)) {
  echo "The domain [not valid - false]";
}else{
  echo "The domain is [valid - true]";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM