Hi
Yeah this can be really horrible at times!!!!
This is a snippet, I would recommend changing this into a function of some sort, just returning true or false.
$regex = "((https?|ftp)\:\/\/)?"; // SCHEME
$regex .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?"; // User and Pass
$regex .= "([a-z0-9-.]*)\.([a-z]{2,3})"; // Host or IP
$regex .= "(\:[0-9]{2,5})?"; // Port
$regex .= "(\/([a-z0-9+\$_-]\.?)+)*\/?"; // Path
$regex .= "(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?"; // GET Query
$regex .= "(#[a-z_.-][a-z0-9+\$_.-]*)?"; // Anchor
$url = 'http://www.domain.dk/seo/friendly/url';
if(preg_match("/^$regex$/", $url))
{
print 'true';
}
mikulucky
Junior Poster in Training
85 posts since Jan 2012
Reputation Points: 41
Solved Threads: 13
I would keep all the of the reg in there, I lifted this from my applications, I keep this as a common function, and I find it is good practise, as you might want to support other url's later on.
The reason it is on sperate lines is because its much easier to read, it could all be on one line, all it is doing adding them together to make one large expression. Each line is split into separate sections of the expression.
Also I find you can't overkill validation. But that is my opinion.
Hope this helps :)
mikulucky
Junior Poster in Training
85 posts since Jan 2012
Reputation Points: 41
Solved Threads: 13
Ok the port section check if there is a port variable, so for example.com:80 or something, but checks that if there is a port, then it must be a valid number. The user and password is if your using apache auth through the URL, this just checks that the username and password are in the correct format. While the HOST IP checks that if the hostname is of a valid type or if just an IP is given this follows IP convention.
mikulucky
Junior Poster in Training
85 posts since Jan 2012
Reputation Points: 41
Solved Threads: 13