Ihave a PHP preg_match regex that is :


The issue is after i attempt to match any URL ending with .com&lifier&lifier , it returns true, but not surprisingly it will return false, what is possibly wrong by using it?

I'm utilizing it as:

    function isValidURL($url) {
        if (preg_match("#^(http:\/\/|https:\/\/|www\.|//)*(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d{1,5}))?([A-Z0-9_-]|\.|\/|\?|\#|=|&|%)*$#i", $url)) {
            return true;
            return false;

and being able to access it as being:

$URL = $_GET['url'];
echo var_dump(isValidURL($URL));

If I open the page as /url.php?url=http://www.google.com it returns true, basically open page as /url.php?url=http://www.google.com** it returns false. Basically open the page as /url.php?url=http://www.google.com&& it returns true however it should return false as .com&lifier&lifier isn't a valid TLD.

Your bit at the finish...


is what's matching the ampersands. That which you most likely wish to accomplish is need a prefixed question mark if there is a query string:


However, there's an easy method to get this done in PHP 5.2+:

var_dump(filter_var('http://example.com', FILTER_VALIDATE_URL));