Which code sample from the above link would you use to satisfy my question number 1 and why particularly that one and not the others ?
And, which one would you use to satisfy my question number 2 and why particularly that one over the others ?

If you need to check just if a domain is valid, you can use regular expressions, as per your link with example.

Regex's are overkill and almost impossible to get completely accurate for this case. Emails are a similar situation too. You're much better off using the built-in PHP functions rather then trying to roll your own in this case.

So you say, that if I try to validate domain voveraitė.lt (which is valid and works), built-in PHP validation will succeed?

That will depend on your system and how you save/transfer the text. If your system handles the encoding correctly for those special characters, then as far as the documentation goes, it should be valid.

Originally Posted by phpmillion

And then, if someone enters 301 Moved Permanently as his "domain", built-in PHP validation will mark it as invalid domain (as author expects)?

No it won't, because that's a valid URL and a valid hostname, so why should it be flagged as invalid? (and you're the one that's mentioned that as invalid, not the OP)

I'd be interested in seeing what regex you can provide that would handle all of these conditions. I'm sure it's possible, but I'm also sure that it would be hard to develop correctly, and would be almost impossible to understand for anyone that's not a regex guru. It would also be a whole lot more processing then the built-in PHP functions.

While I do understand your points, there's always other ways to do things, and in this case it's also a whole lot easier and will give the same results. If you need to filter out specific things (like localhost) then it would be better using separate filters for those so you can see exactly why they are being rejected, instead of one big catch-all "Sorry, that's wrong" message with no other explanation.

Just for interest - would you mind showing a filter_var based code that validates domain voveraitė.lt? You claim If your system handles the encoding correctly for those special characters, then as far as the documentation goes, it should be valid, but PHP documentation says - internationalized domain names (containing non-ASCII characters) will fail

No it won't, because that's a valid URL and a valid hostname, so why should it be flagged as invalid?

Unfortunately, there's a small problem in this. OP stated he needs to validate domain, not URL or hostname, and he even provided an example - When a user inputs his domain name (just the domain name. Eg. devshed.com) into my form field, I want the form to check if the domain is valid

In other words, if URL/hostname is valid, it doesn't mean it's a valid domain. I'm pretty sure it's the main reason why this filter is named FILTER_VALIDATE_URL instead of FILTER_VALIDATE_DOMAIN. Hence, while filter_var is surely the easiest way to validate URLs, I don't think it will work for OP's needs this time.

I missed this thread for nearly a month and surprised to find you 2 kept it alive. Thank you both.
Looking at both of you two's arguments, I realize phpmillion understood my intentions when I opened this thread. Saying all this, if a php built-in function can do the job then I'll give that credit over regex as I hate regex. I'll only opt for regex if the php function is not foolproof.
Anyway, I'm willing to learn both of you two's methods of doing things. Best if you both provide code samples how you'd code it to solve this issue. That way, I and the newbies to this thread learn both tactics from 2 seniors. I hope I'm not asking for too much.

Ok, I've got your answer phpmillion to use dns_get_record() function if I want to check if a domain is functional. No need to provide any code sample for this one unless you want to.
I got Catacaustic code sample at post 6:Validating Domains In Different Ways ?

Now, I'd appreciate a regex code sample from you phpmillion (if that is not too much to ask) to check if the domain is valid or not regardless of whether the url is valid or not excluding the domain. And then, I guess I can say my learning on this issue is complete. Can close thread then.
Thread will be handy to all future php newcomers.

@everyone, now and then I mention Kicken (this forum) and Mlukac89 (from sitepoint.com forum) and Benanamen (this forum and codingforums.com) and Tango Force (codingforums.com) contributed some code samples for my learning.
But, I forgot, phpmillion also contributed code sample once or twice on some forum. Good thing he is prowling this forum because it jogged my memory.
I remember now, I was adding my own code (link click logging) onto Mini Proxy and it was not logging "http" or "https" and phpmillion told me where to add what code sample and it did the job! I owe him my "web app->Android app" copy.
I now owe Catacaustic too for his code sample contribution on post 6.
These people would get a copy of my app for sure since their code samples taught me how to build the app features:
Mlukac89
Kicken
PhpMillion
Catacaustic

These people might get a copy. They did contribute code samples to my php learning but not sure if what I learnt from them got added onto the app or not. Have to check:
Benanamen
Tango Force
Requinix (at another forum)

I think some at other forums contributed code samples too for my learning purpose. Will have to check these forums one day by retracing my footsteps.
Just mentioning all this so you guys understand, I don't forget someone who contributes code samples for my php learning.
And, once you start seeing how my app helps bankrupts, unemployed, house wives, single parents, bankrupt, homeless, etc. then you'd appreciate that you were part of my learning that helped so many classes of people out of poverty. I'm not kidding.
Thanks for reading everyone.

Run that. For 301 Moved Permanently the result is the proper filtered URL, which is correct. For the second invalid URL, it returns false.

So sorry, but the filter_var() option works for your example too, and is still the best option.

Yeah, I just tested it. I get:

Validation result for "http://localhost" is "http://localhost"

Validation result for "http://not a url" is ""

Even though this thread is about domain validation, your url validation has become a bonus for me. Thanks. Made a note of it. Will become handy whilst building my searchengine crawler. Another incomplete project that is still on hold.
Now, I need a domain filter and so do you mind replying to my post just before this one ?