1

I need to validate domain name from text bunch of text string to handle the following:

www.dewalist.com
dewaxxxx.com
ng.dewalist.com
dewaster.com.au
test.co.uk
...
...

I can use FILTER_VALIDATE_URL however this only validates the full url: http://www.dewalist.com bla bla which is not really what I want.

Similar like function but instead for email, it's for domain:

function extract_email_address ($string) 
    {
       $emails = array();
       $string = str_replace("\r\n",' ',$string);
       $string = str_replace("\n",' ',$string);

       foreach(preg_split('/ /', $string) as $token) {
            $email = filter_var($token, FILTER_VALIDATE_EMAIL);
            if ($email !== false) { 
                $emails[] = $email;
            }
        }
        return $emails;
    }
dcpartners
  • 4,354
  • 11
  • 43
  • 64
  • [Possible duplicate](http://stackoverflow.com/questions/2894902/check-for-a-valid-domain-name-in-a-string?rq=1) or [here](http://stackoverflow.com/questions/10306690/domain-name-validation-with-regex). – Mike Oct 01 '13 at 04:25

1 Answers1

1

You can use parse_url() to split URL into logical parts and than validate only part you need by regexp.

http://php.net/parse_url

Yaroslav
  • 2,128
  • 20
  • 32