Fueling Your Coding Mojo

Buckle up, fellow PHP enthusiast! We're loading up the rocket fuel for your coding adventures...

Popular Searches:
60
Q:

Regular expression required for valid domain name check in PHP

Hi everyone,

I am currently working on a project where I need to validate domain names in PHP. I want to ensure that users input a valid domain name before proceeding. Could someone assist me in creating a regular expression to achieve this?

I would like the regular expression to consider the following criteria for a valid domain name:
1. The domain name should consist of alphanumeric characters and hyphens.
2. It should start and end with an alphanumeric character.
3. Each portion of the domain name between the dots should be at least two characters long.
4. The domain name can have multiple subdomains, separated by dots.

For example, the regular expression should match domain names like:
- example.com
- subdomain.example.co.uk
- my-name.com

And it should not match:
- example.1com (only alphanumeric characters and hyphens are allowed)
- subdomain.example.c (the last portion should have at least two characters)

I appreciate any help or suggestions you can provide. Thank you in advance for your assistance!

Best regards,
[Your Name]

All Replies

retha96

Hey there!

I've had my fair share of experience dealing with domain name validation in PHP, and I'd be happy to share my approach with you!

For validating domain names, I found success using the following regular expression pattern:

php
$pattern = '/^(?=.{1,255}$)([a-zA-Z0-9](?:(?:[a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+(?:[a-zA-Z]{2,})$)/';


Now, let me walk you through what this pattern does:

- `^(?=.{1,255}$)`: This component ensures that the entire domain name is between 1 and 255 characters long.
- `([a-zA-Z0-9](?:(?:[a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+)`: This section matches each portion of the domain name, allowing alphanumeric characters and hyphens. It also allows for a minimum length of 1 and a maximum length of 63 characters per portion, while ensuring they are separated by dots.
- `(?:[a-zA-Z]{2,})$)`: This part accounts for the top-level domain (TLD) and requires it to be at least 2 characters long. The `$` symbol ensures that the pattern matches the complete domain name.

You can now use the `preg_match()` function in PHP to utilize this regular expression and validate a domain name:

php
$domain = "example.com";
if (preg_match($pattern, $domain)) {
echo "The domain name is valid!";
} else {
echo "Oops, the domain name is invalid.";
}


Feel free to give this a try and let me know if you encounter any issues or have further questions!

Best regards,
User 2

rmetz

Hey [Your Name],

I've recently faced a similar requirement in PHP, and I was able to solve it using a regular expression. I'll be glad to help you out!

To validate domain names, I used the following regular expression pattern:

php
$pattern = '/^(?=.{1,253}$)([a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]?)(\.[a-zA-Z0-9-]{2,})+$/';


Let's break it down:

- `^(?=.{1,253}$)`: This part ensures that the entire domain name is between 1 and 253 characters long.
- `([a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]?)`: This section matches each portion of the domain name separated by dots. It allows alphanumeric characters and hyphens, with a minimum length of 1 and maximum length of 63 characters per portion.
- `(\.[a-zA-Z0-9-]{2,})+`: This portion matches the domain extension and any additional subdomains. It allows alphanumeric characters and hyphens, with a minimum length of 2 characters.

To use this regular expression in PHP, you can utilize the `preg_match()` function like so:

php
$domain = "example.com";
if (preg_match($pattern, $domain)) {
echo "Valid domain name!";
} else {
echo "Invalid domain name.";
}


This should give you the desired validation for domain names based on your criteria. Let me know if you have any further questions or need additional help!

Cheers,
User 1

New to LearnPHP.org Community?

Join the community