3

I am trying to validate if a domain does have GET parameters with preg_match and and a REGEX, which i require it to have for my purposes.

What I have got working is validating a domain without GET parameters like so:

if (preg_match("/^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}$/", 'domain.com')) {
        echo 'true';
} else {
        echo 'false';
}

I get true for this test.

So far so good. What I am having trouble with is adding in the GET parameters, Amongst a number of REGEX's I have tried with still no luck is the following:

if (preg_match("/^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}([/?].*)?$/", 'domain.com?test=test')) {
        echo 'true';
} else {
        echo 'false';
}

Here i get false returned and hence am not able to validate a domain with GET parameters which are required.

Any assistance will be much appreciated ^^

Regards

Sunjalo
  • 189
  • 1
  • 6

3 Answers3

3

This code is not tested, but I think it should work:

$pattern = "([a-z0-9-.]*)\.([a-z]{2,3})"; //Host
$pattern .= "(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?"; //Get requests
if (preg_match($pattern, 'domain.com?test=test')) {
        echo 'true';
} else {
        echo 'false';
}
Marcus
  • 8,230
  • 11
  • 61
  • 88
1

What is the advantage of using a REGEX?

Why not just

<?php
$xGETS = count($_GET);
if(!$xGETS)
{
    echo 'false';
} else {
    echo 'true';
}

// PHP 5.2+ 

$xGETS = filter_var('http://domain.com?test=test', FILTER_VALIDATE_URL, FILTER_FLAG_QUERY_REQUIRED);
if(!$xGETS)
{
    echo 'false';
} else {
    echo 'true';
}
0

Your first regular expression will reject some valid domain names (e.g. from the museum and travel TLDs and domain names that include upper case letters) and will recognize some invalid domain names (e.g. where a label or the whole domain name is too long).

If this is fine with you, you might just as well search for the first question mark and treat the prefix as domain name and the suffix as "GET parameters" (actually called query string).

If this is not fine with you, a simple regular expression will not suffice to validate domain names, because of the length constraints of domain names and labels.

Oswald
  • 31,254
  • 3
  • 43
  • 68