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.

Leandro Garcia
  • 3,138
  • 11
  • 32
  • 44
  • Do you filter out possible .com's or slashes at the end? – Max Feb 16 '12 at 02: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 –  Feb 16 '12 at 02:45
  • possible duplicate of [How to validate domain name in PHP?](http://stackoverflow.com/questions/1755144/how-to-validate-domain-name-in-php) –  Feb 16 '12 at 02:45

2 Answers2

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.

Brent Baisley
  • 12,641
  • 2
  • 26
  • 39
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.

wecsam
  • 2,651
  • 4
  • 25
  • 46
  • What if `$desired_domain = "www.desireddomain.com.net";`, how can I pick the domain name only? – Leandro Garcia Feb 16 '12 at 03: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 '12 at 03:08
  • For that, you could use `/(^.*?\.|(\..*?){2})/` for the regular expression, but it starts getting tedious after that. The user could put as many `.com`s and `.net`s 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 '12 at 03:12