Organizational Research By

Surprising Reserch Topic

how to validate domain name in php


how to validate domain name in php  using -'php,regex,domain-name'

Is it possible without using regular expression?

For example, I want to check that a string is a valid domain:

domain-name
abcd
example


Are valid domains. These are invalid of course:

domaia@name
ab$%cd


And so on. So basically it should start with an alphanumeric character, then there may be more alnum characters plus also a hyphen. And it must end with an alnum character, too.

If it's not possible, could you suggest me a regexp pattern to do this?

EDIT:

Why doesn't this work? Am I using preg_match incorrectly?

$domain = '@djkal';
$regexp = '/^[a-zA-Z0-9][a-zA-Z0-9\-\_]+[a-zA-Z0-9]$/';
if (false === preg_match($regexp, $domain)) {
    throw new Exception('Domain invalid');
}

    

asked Sep 29, 2015 by mannumits1
0 votes
7 views



Related Hot Questions

15 Answers

0 votes
NULL
answered Sep 29, 2015 by okesh.badhiye
0 votes
NULL
answered Sep 29, 2015 by sameer rathore
0 votes
NULL
answered Sep 29, 2015 by param.oncemore
0 votes
NULL
answered Sep 29, 2015 by rajeshujade
0 votes
NULL
answered Sep 29, 2015 by gauravsinghal83
0 votes
NULL
answered Sep 29, 2015 by sachin wagh
0 votes
NULL
answered Sep 29, 2015 by gauravsinghal83
0 votes
NULL
answered Sep 29, 2015 by sachin wagh
0 votes
NULL
answered Sep 29, 2015 by sandeep bhadauria
0 votes
NULL
answered Sep 29, 2015 by atulpariharmca
0 votes
NULL
answered Sep 29, 2015 by deepak
0 votes
NULL
answered Sep 29, 2015 by bhavin
0 votes
NULL
answered Sep 29, 2015 by maurya
0 votes
NULL
answered Sep 29, 2015 by sanjaypal1983
0 votes
NULL
answered Sep 29, 2015 by vijayshukla80

...