By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,564 Members | 804 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,564 IT Pros & Developers. It's quick & easy.

Validate domain name context

P: n/a
Sam
Validate domain name context

Hello , Today I want to make sure from a given text is a domain style
or not.

for example : the domain can be

example.com or example.com.us or example.us so , what is the pattren
to check the given string is a domain style or not?

note that the first text must be at least 2 chars and the second text
may be 2 or 3 chars and the last text must be 2 chars or null.

Thanks,
Sam Shannaq

Jan 27 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Sam wrote:
Validate domain name context

Hello , Today I want to make sure from a given text is a domain style
or not.

for example : the domain can be

example.com or example.com.us or example.us so , what is the pattren
to check the given string is a domain style or not?

note that the first text must be at least 2 chars and the second text
may be 2 or 3 chars and the last text must be 2 chars or null.

Thanks,
Sam Shannaq
can also be example.info

bill
Jan 27 '07 #2

P: n/a
Sam
yes sure

so any one have the pattren to check?

On Jan 27, 10:17 pm, bill <nob...@spamcop.netwrote:
Sam wrote:
Validate domain name context
Hello , Today I want to make sure from a given text is a domain style
or not.
for example : the domain can be
example.com or example.com.us or example.us so , what is the pattren
to check the given string is a domain style or not?
note that the first text must be at least 2 chars and the second text
may be 2 or 3 chars and the last text must be 2 chars or null.
Thanks,
Sam Shannaqcan also be example.info

bill- Hide quoted text -- Show quoted text -
Jan 27 '07 #3

P: n/a
There's also '.museum' TLD, e.g.

example.museum

Non-national domain names can only have digits, Latin letters and a
dash in them; dash can't be first or last character. So you can begin
with this pattern:

#^(([a-z0-9][-a-z0-9]*?[a-z0-9])\.)+[a-z]{2,6}$#

That will match all valid 3-rd or higher level domain names as well

However, national domain names can have non-ASCII letters.

Best wishes,

Konstantin

On 28 ., 02:12, "Sam" <m.shan...@gmail.comwrote:
yes sure

so any one have the pattren to check?

On Jan 27, 10:17 pm, bill <nob...@spamcop.netwrote:
Sam wrote:
Validate domain name context
Hello , Today I want to make sure from a given text is a domain style
or not.
for example : the domain can be
example.com or example.com.us or example.us so , what is the pattren
to check the given string is a domain style or not?
note that the first text must be at least 2 chars and the second text
may be 2 or 3 chars and the last text must be 2 chars or null.
Thanks,
Sam Shannaqcan also be example.info
bill- Hide quoted text -- Show quoted text -
Jan 28 '07 #4

P: n/a
Sam wrote:
so any one have the pattren to check?
Please don't top post.

How sure do you want to be that a domain is real? Do you just want to make
sure that the domain is lexically sane? If so, you want to check that the
domain name has 2 or more components, each of which contains at least two
alphanumeric characters and may contain hyphens, though the hyphen may not
be the first or last character. Each component should terminate with a dot.
Code here:

$domain = 'me.example.net.';

// Note the domain ends with a dot! This is the correct form for
// a fully qualified domain name, even though most people tend to
// leave out the final dot. Including the dot makes the regular
// expression check much easier, so here we want to make sure that
// $domain ends with a dot, and if not add it.
if (!preg_match('/\.$/', $domain)) $domain .= '.';

// Now check if it meets syntax rules.
$domain_ok = preg_match('/^([a-z0-9][a-z0-9\-]*[a-z0-9]\.){2,}$/',
$domain);

This check will allow for a domain like 'foobar.quux.quuux.' even though
it is obviously not a real domain though. To check that it's a real,
registered domain name, which points to a real IP address, we can use
gethostbyname():

// Firstly, do all the above stuff to make sure it it syntactically
// correct. No point wasting network resources calling gethostbyname()
// for a domain that is obviously invalid. Now:

$domain_ok = $domain_ok && !empty(gethostbyname($domain));

// Now $domain_ok is TRUE iff $domain is an Internet host. Yay!

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Jan 28 '07 #5

P: n/a
Three objections:

1. Not all the registered domain names have valid 'A' record. In that
case, gethostbyname() won't return anything meaningful for registered
domain. You'll have to address WHOIS database directly and that isn't
an easy and quick trick, generally.

2. Domain names check might be required to register that domain.
Again, gethostbyname(0 won't return anything useful.

3. In case the corresponding nameservers are unreachable at the
moment, gethostbyname() won't return anything valid

I only wished to say that gethostbyname() doesn't fit all the cases
and, in general, it makes validation significantly slower.

Best wishes,

Konstantin
This check will allow for a domain like 'foobar.quux.quuux.' even though
it is obviously not a real domain though. To check that it's a real,
registered domain name, which points to a real IP address, we can use
gethostbyname():

// Firstly, do all the above stuff to make sure it it syntactically
// correct. No point wasting network resources calling gethostbyname()
// for a domain that is obviously invalid. Now:

$domain_ok = $domain_ok && !empty(gethostbyname($domain));

// Now $domain_ok is TRUE iff $domain is an Internet host. Yay!

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~http://tobyinkster.co.uk/contact
Jan 28 '07 #6

P: n/a
Sagari wrote:
I only wished to say that gethostbyname() doesn't fit all the cases
and, in general, it makes validation significantly slower.
Please do not top post.

I did mention towards the end, that it actually checks if $domain is a
valid Internet host. Depending on why the OP needs to "validate" the
domain name this may or may not be important.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Jan 28 '07 #7

P: n/a
Sam
When I do

$domainName = "google.com";
$res = ereg('#^(([a-z0-9][-a-z0-9]*?[a-z0-9])\.)+[a-z]{2,6}$#',
$domainName)

res was false :( and I got this warning

Warning: ereg() [function.ereg]: REG_BADRPT

Thanks

On Jan 28, 4:50 am, "Sagari" <konstan...@boyandin.ruwrote:
There's also '.museum' TLD, e.g.

example.museum

Non-national domain names can only have digits, Latin letters and a
dash in them; dash can't be first or last character. So you can begin
with this pattern:

#^(([a-z0-9][-a-z0-9]*?[a-z0-9])\.)+[a-z]{2,6}$#

That will match all valid 3-rd or higher level domain names as well

However, national domain names can have non-ASCII letters.

Best wishes,

Konstantin

On 28 ., 02:12, "Sam" <m.shan...@gmail.comwrote:
yes sure
so any one have the pattren to check?
On Jan 27, 10:17 pm, bill <nob...@spamcop.netwrote:
Sam wrote:
Validate domain name context
Hello , Today I want to make sure from a given text is a domain style
or not.
for example : the domain can be
example.com or example.com.us or example.us so , what is the pattren
to check the given string is a domain style or not?
note that the first text must be at least 2 chars and the second text
may be 2 or 3 chars and the last text must be 2 chars or null.
Thanks,
Sam Shannaqcan also be example.info
bill- Hide quoted text -- Show quoted text -- Hide quoted text -- Show quoted text -
Jan 28 '07 #8

P: n/a
..oO(Sam)
>When I do

$domainName = "google.com";
$res = ereg('#^(([a-z0-9][-a-z0-9]*?[a-z0-9])\.)+[a-z]{2,6}$#',
$domainName)

res was false :( and I got this warning

Warning: ereg() [function.ereg]: REG_BADRPT
Forget ereg*(), use the PCRE functions instead. They are much more
flexible and the preferred regex engine in PHP 6.

Micha
Jan 28 '07 #9

P: n/a
Sam wrote:
ereg('#^(([a-z0-9][-a-z0-9]*?[a-z0-9])\.)+[a-z]{2,6}$#',$domainName)
IIRC, the ereg() function doesn't expect any starting or ending characters
the the regular expression (the hash marks in the above).

Use preg_match() -- it's faster, more flexible and expects the starting and
ending characters!

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/CSS/Javascript/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Jan 29 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.