Showing posts with label validate url. Show all posts
Showing posts with label validate url. Show all posts

Monday, June 26, 2017

Validating a URL in PHP

Check if the variable $url is a valid URL:

The FILTER_VALIDATE_URL filter validates a URL.

Possible  flags:

FILTER_FLAG_SCHEME_REQUIRED - URL must be RFC compliant.
Like http://example

FILTER_FLAG_HOST_REQUIRED - URL must include host name.
Like http://www.example.com

FILTER_FLAG_PATH_REQUIRED - URL must have a path after domain.
Like www.example.com/example1/

FILTER_FLAG_QUERY_REQUIRED - URL must have a query string.
Like "example.php?name=Peter&age=37"

filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED)
filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED)
filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED)
filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_QUERY_REQUIRED)


Wednesday, June 3, 2015

Validate URL Using Java & Regex


class UrlValidator {
    public static void main (String[] args) {
        check("http://google.com");
        check("http://google.com?a=b#99");
        check("http://google.com?a=b#99#sty");
        check("http://www.google.com?a=1&b=2 / 3");
        check("http://www.google.com?a=1&orderRate=orderRateUUID");
    }

    public static void check(String url) {
        String Regex = "^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";

        Boolean matches = url.matches(Regex);

        if(matches) {
            System.out.println("URL (YES): " + url);
        }
        else {
            System.out.println("URL (NOT): " + url);
        }
    }
}


URL (YES): http://google.com
URL (YES): http://google.com?a=b#99
URL (YES): http://google.com?a=b#99#sty
URL (NOT): http://www.google.com?a=1&b=2 / 3
URL (YES): http://www.google.com?a=1&orderRate=orderRateUUID