Created
March 19, 2013 20:55
-
-
Save mintindeed/5200060 to your computer and use it in GitHub Desktop.
Determine whether a string is trying to be a URL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Determine whether a string is trying to be a URL | |
* | |
* @see http://daringfireball.net/2010/07/improved_regex_for_matching_urls | |
* | |
* @param string $url | |
* @return bool preg_match() returns 1 if the pattern matches given subject, 0 if it does not, or FALSE if an error occurred. | |
*/ | |
function is_valid_url( $url ) { | |
return preg_match( "~(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))~", $url ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment