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 used this on a few projects, i don't believe i've run into issues, but i'm sure it's not exhaustive:
most of the random junk at the end is to deal with situations like |
|||||||||||||
|
Use the
It is bad practice to use regular expressions when not necessary. |
|||||||||||||||||||||
|
As per the PHP manual - parse_url should not be used to validate a URL. Unfortunately, it seems that Both Therefore in this case - regex is the better method. |
|||||||||||
|
Just in case you want to know if the url really exists:
|
|||||||||
|
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 |
||||
|
|
|||
|
I've used this one with good success - I don't remember where I got it from
|
|||||
|
Edit:
Just my two cents but I've developed this function and have been using it for a while with success. It's well documented and separated so you can easily change it.
|
|||||||
|
|
|||
|
As per John Gruber (Daring Fireball): Regex:
using in preg_match():
Here is the extended regex pattern (with comments):
For more details please look at: http://daringfireball.net/2010/07/improved_regex_for_matching_urls |
|||
|
And there is your answer =) Try to break it, you can't!!!
|
||||
|
I used this one for quite some time with good rate of success:
Since it's been in use for a few years it might need to be updated for some newer TLDs though. |
|||||||
|
Peter's Regex doesn't look right to me for many reasons. It allows all kinds of special characters in the domain name and doesn't test for much. Frankie's function looks good to me and you can build a good regex from the components if you don't want a function, like so:
Untested but I think that should work. Also, Owen's answer doesn't look 100% either. I took the domain part of the regex and tested it on a Regex tester tool http://erik.eae.net/playground/regexp/regexp.html I put the following line:
in the "regexp" section and the following line:
under the "sample text" section. The result allowed the minus character through. Because \S means any non-space character. Note the regex from Frankie handles the minus because it has this part for the first character:
Which won't allow the minus or any other special character. |
|||
|
As per Vikash Kumar's code, tested with
|
|||
|
I've found this to be the most useful for matching a URL..
|
|||
|
This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.