Guest User

url_detector

a guest
Mar 18th, 2010
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.66 KB | None | 0 0
  1. // 2 stage url finder
  2. // known issue: detects URLs ending with backslash
  3. // (should drop those before returning)
  4. // separates urls with urls inside them eg. URL query
  5. // string that has the string 'http' as a parameter
  6. // is returned as two
  7. function url_detect($str='')
  8. {
  9.     if ($str==='') return $str;
  10.     $not = '-+.,<>:;()\[\]{}"';
  11.     // regex explanation: do not start with char from the not list,
  12.     // capture an optional https?://, then again another not list,
  13.     // any alphanumerics or dash (gather possesively ++), alnum or
  14.     // punct (optional because the first alnum-dash group might be
  15.     // enough), then do not end with any string from the not list
  16.     // oh, and do it all caselessly
  17.     $p = '#(?P<all>(?!['.$not.'])(?P<protocol>https?://)?(?!['.$not.'])(?P<url_string>[-[:alnum:]]++\.(?P<ignore>[[:alnum:][:punct:]]+)?)(?<!['.$not.']))#i';
  18.     //pe($p);
  19.    
  20.     $cnt = preg_match_all($p,$str,$stage_1,PREG_SET_ORDER);
  21.    
  22.     //p($stage_1);
  23.    
  24.     $tlds = '(COM|ORG|EDU|GOV|UK|NET|CA|DE|JP|FR|AU|US|RU|CH|IT|NL|SE|NO|ES|MIL|FI|CN|BR|BE|AT|INFO|PL|DK|CZ|CL|HU|NZ|IL|IE|ZA|TW|KR|MX|GR|AR|HK|IN|PT|SG|TR|SK|RO|TV|LV|BIZ|UA|EE|TH|HR|LT|IS|NU|VU|LU|MY|FM|SI|CO|NI|PH|CC|LI|BG|AE|YU|MD|NAME|PK|VE|MA|PW|WS|EG|MK|IR|ID|BZ|SA|BA|PE|KZ|TC|UZ|EC|BY|CY|ST|TO|VN|GE|TK|MS|AM|AC|CR|AD|AF|AG|AI|AL|AN|AO|AQ|PA|AS|AA|AW|AX|AZ|BB|BD|BF|BH|BI|BJ|BM|BN|BO|BS|BT|BV|BW|CD|CF|CG|CI|CK|CM|OP|CU|CV|CX|DJ|DM|DO|DZ|ER|ET|EU|FJ|FK|FO|GA|GB|GD|GF|GG|GH|GI|GL|GM|GN|GP|GQ|GS|GT|GU|GW|GY|HM|HN|HT|IM|IT|IO|IQ|JE|JM|JO|JOBS|KE|KG|KH|KI|KM|KN|KP|KW|KY|LA|LB|LC|LK|LR|LS|LY|MC|ME|MG|MH|ML|MM|MN|MO|MOBI|MP|MQ|MR|MT|MU|MUSEUM|MV|MW|MZ|NA|NC|NE|NF|NG|NP|NR|OM|PA|PF|PG|PM|PN|PR|PRO|PS|PY|QA|RE|RS|RW|SB|SC|SD|SH|SJ|SL|SM|SN|SO|SR|SU|SV|SY|SZ|TD|TEL|TF|TG|TJ|TL|TM|TN|TP|AL|TT|TZ|UG|UY|VA|VC|VG|VI|WF|YE|YT|ZM|ZW)($|\/)';
  25.     // the old way, left here for comparison
  26.     //$url_p = '#[-[:alnum:]]++[.][a-z]{2,6}#';
  27.     $url_p = '#[-[:alnum:]]+[.]'.$tlds.'#i';
  28.  
  29.     // send the captures to a second stage of processing   
  30.     $return_array = array();
  31.     $key=0;
  32.     foreach ($stage_1 as $matches)
  33.     {
  34.         //pe($matches);
  35.    
  36.         // check if the url is real or needs more processing
  37.         $is_url = preg_match($url_p, $matches['url_string']);
  38.         if (! $is_url) continue;
  39.        
  40.         // if we have http:// strings inside a domain name
  41.         // that were caught by the first stage regex, split them
  42.         // for example: http://domain.comhttp://anotherdom.com
  43.         // use the PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE flags to make sure
  44.         // we can rebuild the scheme properly
  45.         // this code will cause a problem with links that
  46.         // send http:// strings in the query part like this:
  47.         // http://example.com/?url=http://example.org
  48.         $url_parts = preg_split('#(https?://)#',$matches['all'],-1,PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);
  49.         $cup =count($url_parts)-1;
  50.         // if preg_split() finds more than two http strings
  51.         // then url_parts array has more than 2 array items
  52.         if ($cup > 1)
  53.         {
  54.             // rebuild them as space separated string and
  55.             // call the url_detect() function again
  56.             $new_urls = '';
  57.             for($k=0; $k<$cup; $k++)
  58.             {
  59.                 $new_urls .= $url_parts[$k].$url_parts[$k+1].' ';
  60.                 $k++;
  61.             }
  62.             // recurse and return/build up the values
  63.             // into the main return_array
  64.             $arr = url_detect($new_urls);
  65.             //p($arr);
  66.             // each array item has a protocol and host_domain
  67.             foreach($arr as $a)
  68.             {
  69.                 $return_array[$key]['protocol']=$a['protocol'];
  70.                 $return_array[$key]['url_string']=$a['url_string'];
  71.                 $key++;
  72.             }
  73.             continue;
  74.         }
  75.        
  76.         // database table default protocol is http://
  77.         $return_array[$key]['protocol']=$matches['protocol'];
  78.         $return_array[$key]['url_string']=$matches['url_string'];  
  79.         $key++;
  80.     }
  81.     return $return_array;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment