I've been looking for a simple regex for URL's, does anybody have one handy that works well? I didn't find one with the zend framework validation classes and have seen several implementations.
Thanks
I've been looking for a simple regex for URL's, does anybody have one handy that works well? I didn't find one with the zend framework validation classes and have seen several implementations.
Thanks
I don't think that using regular expressions is a smart thing to do in this case. It is impossible to match all of the possibilities and even if you did, there is still a chance that url simply doesn't exist.
Here is a very simple way to test if url actually exists and is readable :
(if there is no
preg_match
then this would also validate all filenames on your server)I've used this one with good success - I don't remember where I got it from
Here's a simple class for URL Validation using RegEx and then cross-references the domain against popular RBL (Realtime Blackhole Lists) servers:
Install:
Usage:
Add a URL as the parameter of the
domain()
method and check the the return.Output:
As you can see above, www.bokranzr.com is listed as malicious website via an RBL so the domain was returned as false.
OK, so this is a little bit more complex then a simple regex, but it allows for different types of urls.
Examples:
All which should be marked as valid.
Note that there is a in_array check for the protocols that you want to allow (currently only http and https are in that list).
Here is the way I did it. But I want to mentoin that I am not so shure about the regex. But It should work thou :)
This way you won't need the eval marker on your pattern.
Hope it helps :)