How to validate a domain name using Regex & PHP?
How about:
^(?:[-A-Za-z0-9]+\.)+[A-Za-z]{2,6}$
Regex to validate domain name without top-level domain using preg_match
The answer to the problem is changing the pattern to:
$pattern = "(\A[a-zA-Z0-9](?:[a-zA-Z0-9\d-]*[a-zA-Z0-9])+\z)";
Which doesn't allow hyphens at the beginning or the end of the domain, doesn't allow any TLDs, and any other special characters.
Validate a domain name with GET parameters using a REGEX
This code is not tested, but I think it should work:
$pattern = "([a-z0-9-.]*)\.([a-z]{2,3})"; //Host
$pattern .= "(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?"; //Get requests
if (preg_match($pattern, 'domain.com?test=test')) {
echo 'true';
} else {
echo 'false';
}
What is a regular expression which will match a valid domain name without a subdomain?
Well, it's pretty straightforward a little sneakier than it looks (see comments), given your specific requirements:
/^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\.[a-zA-Z]{2,}$/
But note this will reject a lot of valid domains.
php preg_match validate domain
if (preg_match("!"#$%&\'()*+,-./@:;<=>[\\]^_`{|}~", $myString))
{
//valid url
}
This should do...
Regex for specific domain name
Now instead of using regex
you can simply use strstr
function of PHP like as
$email = "xyz@xyz.com";
$email2 = "xyz@xyz.net";
$valid_domain = "@xyz.com";
function checkValidDomain($email, $valid_domain){
if(!filter_var($email,FILTER_VALIDATE_EMAIL) !== false){
if(strstr($email,"@") == $valid_domain){
return "Valid";
}else{
return "Invalid";
}
}else{
return "Invalid Email";
}
}
echo checkValidDomain($email, $valid_domain);// Valid
echo checkValidDomain($email2, $valid_domain);// Invalid
Why I didn't used regex over here you can read many of those threads on SO too Email validation using regular expression in PHP and Using a regular expression to validate an email address
Php regular expression, url validation, following dots in domain name is valid?
You can use this version of your regex:
^(http:[\/]{2})((?![^\/]*?\.{2}[^\/]*?)[\w\d\-\_\.]+)(\/(?:[\/\w]+)?)?$
The problem was with the \W
(together with \w
in the same character class) that matched everything, even a new line.
Regular expression required for valid domain name check in PHP
I guess this is what youre searching for:
^[a-zA-Z0-9]+([a-zA-Z0-9-.]+)?.(com|org|net|mil|edu|de|COM|ORG|NET|MIL|EDU|DE)$
Related Topics
Using Ajax to Pass Variable to PHP and Retrieve Those Using Ajax Again
How to Serve Documents from Outside the Web Root Using PHP
Output Text File with Line Breaks in PHP
Xml Parse Error - Extra Content at the End of the Document
Mysqli Prepared Statement Column with Variable
What Encryption Algorithm Is Best for Encrypting Cookies
How to Change PHP-Cli Version on Ubuntu 14.04
How to Validate Google Recaptcha on Form Submit
Wordpress: Loading Multiple Scripts with Enqueue
Oauth Implementation in Netsuite Using PHP
How to Get the Last N Items in a PHP Array as Another Array
PHP Try-Catch Blocks: Are They Able to Catch Invalid Arg Types
Is There a Built-In Function or Plugin to Handle Date Formatting in JavaScript