Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 2 stage url finder
- // known issue: detects URLs ending with backslash
- // (should drop those before returning)
- // separates urls with urls inside them eg. URL query
- // string that has the string 'http' as a parameter
- // is returned as two
- function url_detect($str='')
- {
- if ($str==='') return $str;
- $not = '-+.,<>:;()\[\]{}"';
- // regex explanation: do not start with char from the not list,
- // capture an optional https?://, then again another not list,
- // any alphanumerics or dash (gather possesively ++), alnum or
- // punct (optional because the first alnum-dash group might be
- // enough), then do not end with any string from the not list
- // oh, and do it all caselessly
- $p = '#(?P<all>(?!['.$not.'])(?P<protocol>https?://)?(?!['.$not.'])(?P<url_string>[-[:alnum:]]++\.(?P<ignore>[[:alnum:][:punct:]]+)?)(?<!['.$not.']))#i';
- //pe($p);
- $cnt = preg_match_all($p,$str,$stage_1,PREG_SET_ORDER);
- //p($stage_1);
- $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)($|\/)';
- // the old way, left here for comparison
- //$url_p = '#[-[:alnum:]]++[.][a-z]{2,6}#';
- $url_p = '#[-[:alnum:]]+[.]'.$tlds.'#i';
- // send the captures to a second stage of processing
- $return_array = array();
- $key=0;
- foreach ($stage_1 as $matches)
- {
- //pe($matches);
- // check if the url is real or needs more processing
- $is_url = preg_match($url_p, $matches['url_string']);
- if (! $is_url) continue;
- // if we have http:// strings inside a domain name
- // that were caught by the first stage regex, split them
- // for example: http://domain.comhttp://anotherdom.com
- // use the PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE flags to make sure
- // we can rebuild the scheme properly
- // this code will cause a problem with links that
- // send http:// strings in the query part like this:
- // http://example.com/?url=http://example.org
- $url_parts = preg_split('#(https?://)#',$matches['all'],-1,PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);
- $cup =count($url_parts)-1;
- // if preg_split() finds more than two http strings
- // then url_parts array has more than 2 array items
- if ($cup > 1)
- {
- // rebuild them as space separated string and
- // call the url_detect() function again
- $new_urls = '';
- for($k=0; $k<$cup; $k++)
- {
- $new_urls .= $url_parts[$k].$url_parts[$k+1].' ';
- $k++;
- }
- // recurse and return/build up the values
- // into the main return_array
- $arr = url_detect($new_urls);
- //p($arr);
- // each array item has a protocol and host_domain
- foreach($arr as $a)
- {
- $return_array[$key]['protocol']=$a['protocol'];
- $return_array[$key]['url_string']=$a['url_string'];
- $key++;
- }
- continue;
- }
- // database table default protocol is http://
- $return_array[$key]['protocol']=$matches['protocol'];
- $return_array[$key]['url_string']=$matches['url_string'];
- $key++;
- }
- return $return_array;
- }
Advertisement
Add Comment
Please, Sign In to add comment