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

3
  • Is there a reason that you want to use regexps instead of filter_var and FILTER_VALIDATE_URL? Commented Oct 3, 2012 at 8:38
  • hi yes the reason is because it is for a symfony routing yml file which requires you to use a regex :) Commented Oct 3, 2012 at 10:05
  • For domain validation regex, see stackoverflow.com/a/16491074/112731 Commented Feb 6, 2015 at 13:37

3 Answers 3

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';
}
Sign up to request clarification or add additional context in comments.

Comments

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';
}

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.