dd

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

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

        if (strpos($str, '/')===0) {
            return $scheme.'://'.$host.$str;
        } else {
            $part = explode('/', $path);
            array_shift($part);
            $count = substr_count($str, '../');
            if ($count>0) {
                for ($i=0; $i<=$count; $i++) {
                    array_pop($part);
                }
            }
            $path = implode('/', $part);
            $str = str_replace(array('../','./'), '', $str);
            $path = $path=='' ? '/' : '/'.trim($path,'/').'/';
            return $scheme.'://'.$host.$path.$str;
        }        
    }

}
dd