A Way to Check if a String is Found in Third+ Level Domain

Discussion in 'PHP' started by ColorWP.com, Apr 19, 2013.

  1. #1
    I have an input where a user may enter his domain or subdomain.

    What's I'm trying to accomplish is to have a list of bad/blocked words that should not be allowed as a second level domain. They should be allowed as third, fourth or more level domain though.

    For example, if the blocked word is "google", the user should not be allowed to enter:
    googleplus.com
    googlesite.co.uk
    mygoogleblog.org

    Whereas he should be able to enter
    google.myhosting.com
    ilovegoogle.blogparadise.blogs.com
     
    ColorWP.com, Apr 19, 2013 IP
  2. rainborick

    rainborick Well-Known Member

    Messages:
    424
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    120
    #2
    Try:
    
     $domains = array('nogoog.com', 'otherword.com', 'www.googleplus.com', 'googlesite.co.uk',
        'mygoogleblog.org', 'google.myhosting.com', 'ilovegoogle.blogparadise.blogs.com');
     $badWords = array('google', 'other', 'additional');
      foreach ($domains as $domain) {
       echo ($domain);
       $parts = explode('.', $domain);
       $index = 0;
       if (preg_match('/^www\./', $domain)) { $index = 1; }
       $subdomain = $parts[$index];
       echo(" - $subdomain");
       if (testName($subdomain)) { echo(" - Good"); } else { echo(" - BAD!"); }
       echo("<br />\n");
      } // end foreach $domain
    
     function testName($subdomain) {
      global $badWords;
      $result = 1;
       foreach ($badWords as $badWord) {
         if ((strpos($subdomain, $badWord)) !== false) {
          $result = preg_match('/^(.*)' . $badWord . '$/i', $subdomain);
         } // endif strpos()
        if (!$result) { break; }
       } // end foreach $badWords
      return $result;
     } // end testName()
    
    Code (markup):
    The code depends on properly extracting the subdomain name from the URL. My example skips any leading "www.", but you may need to modify it further.
     
    rainborick, Apr 19, 2013 IP