code_junkie

PHP validation/regex for URL

Nov 11th, 2011
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. $text = preg_replace("
  2. #((http|https|ftp)://(S*?.S*?))(s|;|)|]|[|{|}|,|"|'|:|<|$|.s)#ie",
  3. "'<a href="$1" target="_blank">$3</a>$4'",
  4. $text
  5. );
  6.  
  7. var_dump(filter_var('example.com', FILTER_VALIDATE_URL));
  8.  
  9. $pattern = "/b(?:(?:https?|ftp)://|www.)[-a-z0-9+&@#/%?=~_|!:,.;]*[-a-z0-9+&@#/%=~_|]/i";
  10.  
  11. // Checks if string is a URL
  12. // @param string $url
  13. // @return bool
  14. function isURL($url = NULL) {
  15. if($url==NULL) return false;
  16.  
  17. $protocol = '(http://|https://)';
  18. $allowed = '([a-z0-9]([-a-z0-9]*[a-z0-9]+)?)';
  19.  
  20. $regex = "^". $protocol . // must include the protocol
  21. '(' . $allowed . '{1,63}.)+'. // 1 or several sub domains with a max of 63 chars
  22. '[a-z]' . '{2,6}'; // followed by a TLD
  23. if(eregi($regex, $url)==true) return true;
  24. else return false;
  25. }
  26.  
  27. function url_exist($url){//se passar a URL existe
  28. $c=curl_init();
  29. curl_setopt($c,CURLOPT_URL,$url);
  30. curl_setopt($c,CURLOPT_HEADER,1);//get the header
  31. curl_setopt($c,CURLOPT_NOBODY,1);//and *only* get the header
  32. curl_setopt($c,CURLOPT_RETURNTRANSFER,1);//get the response as a string from curl_exec(), rather than echoing it
  33. curl_setopt($c,CURLOPT_FRESH_CONNECT,1);//don't use a cached version of the url
  34. if(!curl_exec($c)){
  35. //echo $url.' inexists';
  36. return false;
  37. }else{
  38. //echo $url.' exists';
  39. return true;
  40. }
  41. //$httpcode=curl_getinfo($c,CURLINFO_HTTP_CODE);
  42. //return ($httpcode<400);
  43. }
  44.  
  45. if (preg_match("#^https?://.+#", $link) and @fopen($link,"r")) echo "OK";
  46.  
  47. function is_valid_url ($url="") {
  48.  
  49. if ($url=="") {
  50. $url=$this->url;
  51. }
  52.  
  53. $url = @parse_url($url);
  54.  
  55. if ( ! $url) {
  56.  
  57.  
  58. return false;
  59. }
  60.  
  61. $url = array_map('trim', $url);
  62. $url['port'] = (!isset($url['port'])) ? 80 : (int)$url['port'];
  63. $path = (isset($url['path'])) ? $url['path'] : '';
  64.  
  65. if ($path == '') {
  66. $path = '/';
  67. }
  68.  
  69. $path .= ( isset ( $url['query'] ) ) ? "?$url[query]" : '';
  70.  
  71.  
  72.  
  73. if ( isset ( $url['host'] ) AND $url['host'] != gethostbyname ( $url['host'] ) ) {
  74. if ( PHP_VERSION >= 5 ) {
  75. $headers = get_headers("$url[scheme]://$url[host]:$url[port]$path");
  76. }
  77. else {
  78. $fp = fsockopen($url['host'], $url['port'], $errno, $errstr, 30);
  79.  
  80. if ( ! $fp ) {
  81. return false;
  82. }
  83. fputs($fp, "HEAD $path HTTP/1.1rnHost: $url[host]rnrn");
  84. $headers = fread ( $fp, 128 );
  85. fclose ( $fp );
  86. }
  87. $headers = ( is_array ( $headers ) ) ? implode ( "n", $headers ) : $headers;
  88. return ( bool ) preg_match ( '#^HTTP/.*s+[(200|301|302)]+s#i', $headers );
  89. }
  90.  
  91. return false;
  92. }
  93.  
  94. ^(http://|https://)(([a-z0-9]([-a-z0-9]*[a-z0-9]+)?){1,63}.)+[a-z]{2,6}
  95.  
  96. (S*?.S*?)
  97.  
  98. [a-z0-9]
  99.  
  100. (?i)b((?:https?://|wwwd{0,3}[.]|[a-z0-9.-]+[.][a-z]{2,4}/)(?:[^s()<>]+|(([^s()<>]+|(([^s()<>]+)))*))+(?:(([^s()<>]+|(([^s()<>]+)))*)|[^s`!()[]{};:'".,<>?«»“”‘’]))
  101.  
  102. preg_match("/(?i)b((?:https?://|wwwd{0,3}[.]|[a-z0-9.-]+[.][a-z]{2,4}/)(?:[^s()<>]+|(([^s()<>]+|(([^s()<>]+)))*))+(?:(([^s()<>]+|(([^s()<>]+)))*)|[^s`!()[]{};:'".,<>?«»“”‘’]))/", $url)
  103.  
  104. (?xi)
  105. b
  106. ( # Capture 1: entire matched URL
  107. (?:
  108. https?:// # http or https protocol
  109. | # or
  110. wwwd{0,3}[.] # "www.", "www1.", "www2." … "www999."
  111. | # or
  112. [a-z0-9.-]+[.][a-z]{2,4}/ # looks like domain name followed by a slash
  113. )
  114. (?: # One or more:
  115. [^s()<>]+ # Run of non-space, non-()<>
  116. | # or
  117. (([^s()<>]+|(([^s()<>]+)))*) # balanced parens, up to 2 levels
  118. )+
  119. (?: # End with:
  120. (([^s()<>]+|(([^s()<>]+)))*) # balanced parens, up to 2 levels
  121. | # or
  122. [^s`!()[]{};:'".,<>?«»“”‘’] # not a space or one of these punct chars
  123. )
  124. )
Add Comment
Please, Sign In to add comment