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
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.