照目录的方式写的,思路较乱,但满足你的要求我想还是有的
function fun1($url,$path)
{
if (substr($path,-1,1)=="/")
{
$path=substr($path,0,-1);
}
if (substr($path,0,1)=="/")
{
$path=substr($path,1);
$root=true;
}
else
{
$root=false;
}
$path=explode("/",$path);
$path_count=count($path);
$num=0;
$str="";
for($i=0;$i<$path_count;$i++)
{
if ($path[$i]=="..")
{
$num++;
}
else
{
if ($path[$i]!=".")
{
$str.=$path[$i]."/";
}
}
}
$url=substr($url,7);
if (substr($url,-1,1)=="/")
{
$url=substr($url,0,-1);
}
$url=explode("/",$url);
$num2=count($url);
$num3=$num2-$num;
$temp="";
if ($root)
{
$temp.=$url[0]."/";
}
else
{
for($i=0;$i<$num3;$i++)
{
$temp.=$url[$i]."/";
}
}
$temp.=$str;
$temp="http://".$temp;
return $temp;
}
例子:echo fun1("http://www.aaa.com/bbb/ccc/asdfsadf/","../imgae/a/");
echo fun1("http://www.aaa.com/bbb/ccc/asdfsadf/","../imgae/a");
echo fun1("http://www.aaa.com/bbb/ccc/asdfsadf","../imgae/a/");
echo fun1("http://www.aaa.com/bbb/ccc/asdfsadf","../imgae/a");
以上均输出:http://www.aaa.com/bbb/ccc/imgae/a/
echo fun1("http://www.aaa.com/bbb/ccc/asdfsadf/","/../imgae/a/");
echo fun1("http://www.aaa.com/bbb/ccc/asdfsadf/","/../../imgae/a/");
echo fun1("http://www.aaa.com/bbb/ccc/","/../imgae/a/");
以上均输出:http://www.aaa.com/imgae/a/echo fun1("http://www.aaa.com/bbb/ccc/asdfsadf/","../../imgae/a/");
输出:http://www.aaa.com/bbb/imgae/a/