Skip to content

Instantly share code, notes, and snippets.

@mintindeed
Created March 19, 2013 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mintindeed/5200060 to your computer and use it in GitHub Desktop.
Save mintindeed/5200060 to your computer and use it in GitHub Desktop.
Determine whether a string is trying to be a URL
<?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