Kelvin
Kelvin

Reputation: 1114

Validation flexible valid URI

i would to make a validation the URI is valid or not. I know this question has been ask before but i could find the flexible one, which it countain some rule

  1. should put http:// or https://
  2. validate host is true and flexible ex : localhost, example.com, www.example.com, sub.domain.com, 198.123.456.678
  3. should contain top-level domain (such as .com or .org and any else)

if the rule break, the response should tell which rule is false

so if the url :

https://example.com, http://localhost, it would return true

example.com should be false, and the response is domain should be the domain must use either http or https

http://www.example should be false, because the domain does not include top-level domain

https://198.123.456.678 and this one should be true too

those are what i expect

thank you

Upvotes: 4

Views: 333

Answers (1)

Kerri
Kerri

Reputation: 455

Since you're using PHP, you can use [FILTER_VALIDATE_URL].

<?php
  $uri = "https://www.stackoverflow.com" ;

  if(filter_var($uri, FILTER_VALIDATE_URL)) {
    echo("$uri is valid") ;
  }
?>

See http://php.net/manual/en/filter.filters.validate.php for additional flags.

Upvotes: 1

Related Questions