根据当前页面url进行地址补全(采集用)

jerry thinkphp 2015年11月19日 收藏
根据当前页面url进行地址补全(采集用)
  1. function formaturl($url, $str){
  2.     if (is_array($str)) {
  3.         $return = array();
  4.         foreach ($str as $href) {
  5.             $return[] = formaturl($url, $href);
  6.         }
  7.         return $return;
  8.     } else {
  9.         if (stripos($str, 'http://')===0 || stripos($str, 'ftp://')===0) {
  10.             return $str;
  11.         }
  12.         $str = str_replace('\\', '/', $str);
  13.         $parseUrl = parse_url(dirname($url).'/');
  14.         $scheme = isset($parseUrl['scheme']) ? $parseUrl['scheme'] : 'http';
  15.         $host = $parseUrl['host'];
  16.         $path = isset($parseUrl['path']) ? $parseUrl['path'] : '';
  17.         $port = isset($parseUrl['port']) ? $parseUrl['port'] : '';

  18.         if (strpos($str, '/')===0) {
  19.             return $scheme.'://'.$host.$str;
  20.         } else {
  21.             $part = explode('/', $path);
  22.             array_shift($part);
  23.             $count = substr_count($str, '../');
  24.             if ($count>0) {
  25.                 for ($i=0; $i<=$count; $i++) {
  26.                     array_pop($part);
  27.                 }
  28.             }
  29.             $path = implode('/', $part);
  30.             $str = str_replace(array('../','./'), '', $str);
  31.             $path = $path=='' ? '/' : '/'.trim($path,'/').'/';
  32.             return $scheme.'://'.$host.$path.$str;
  33.         }        
  34.     }

  35. }