0

I have a field in which I ask for desired domain name, now it came to a point that some user includes www.desireddomain on the field. Having this value, the domain registrar API I have will return an error. The approach I have in mind is to omit any characters before period (including period), using preg_replace(). I have a <select> which contains (.com,.biz,.org,.net)

$desired_domain = "www.desireddomain"; // user input
$will_be = "desireddomain"; // final output after preg_replace() ?

And also, does this limits me to only the period or is there anything more I should know? Thanks.

3
  • Do you filter out possible .com's or slashes at the end?
    – Max
    Feb 16, 2012 at 2:44
  • 2
    there's quite a lot they could enter in the form that would be invalid, you need to robustly validate the user input
    – user557846
    Feb 16, 2012 at 2:45
  • possible duplicate of How to validate domain name in PHP?
    – user557846
    Feb 16, 2012 at 2:45

2 Answers 2

1

You don't need to use regular expressions since you are aren't doing a "fuzzy" search. Just get everything from the last period on, if there is a period.

if ( strrpos('www.domain','.')!==false ) {
    $domain = substr('www.domain',strrpos('www.domain','.')+1);
}

Alternatively, if you want to make sure they didn't enter .com,.biz,.org,.net, use explode and do a check.

$parts = explode('.', 'www.desireddomain.com.net');
$domain = array_pop($parts);
$invalid_parts = array('com','biz','org','net');
if ( in_array($domain, $invalid_parts) && count($parts)>0 ) {
    $domain = array_pop($parts);
} else {
    // No valid domain submitted
}

In this example, they are asking to use "com" as the domain, even though it's no valid. You could create a loop instead, where you continuously pop items off the array until you find a valid part or run out of items.

2
  • Thanks for this, however what if the input is "www.ddmomain.com.net"? Feb 16, 2012 at 3:11
  • As per your question, .com/.net/.org are in a drop down, so the domain would be "net", which my example returns. www.ddmomain.com would be the subdomains. Feb 16, 2012 at 3:33
0

Use the regular expression /^.*\./, as in:

$desired_domain = "www.desireddomain";
$will_be = preg_replace("/^.*\./","",$desired_domain);
print $will_be; //Outputs "desireddomain"

Keep in mind, however, that there are plenty of varieties of invalid data that the user could type in. You are better off telling the user a set of restrictions and then checking them against a regular expression for a valid input.

3
  • What if $desired_domain = "www.desireddomain.com.net";, how can I pick the domain name only? Feb 16, 2012 at 3:02
  • @boy: you really need to filter out a lot of things- www or HTML at the begining, or say a ';-- ... And if someone pastes in a full URL as well
    – Max
    Feb 16, 2012 at 3:08
  • For that, you could use /(^.*?\.|(\..*?){2})/ for the regular expression, but it starts getting tedious after that. The user could put as many .coms and .nets at the end of that as he or she wants. You could just tell the user to type in only the desired domain and validate it against /\w+/.
    – wecsam
    Feb 16, 2012 at 3:12

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.