php 模拟返回url上一层目录
Posted Technofiend
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 模拟返回url上一层目录相关的知识,希望对你有一定的参考价值。
function parseUrl($url, $path)
if (preg_match('/^javascript/', $path))
return $path;
else
if (strpos($path, '/') === 0)
$r = parse_url($url);
$newurl = $r['scheme'] . '://' . $r['host'];
return parseUrl($newurl, ltrim($path, '/'));
$url = rtrim($url, '/') . '/';
do
preg_match('/^(?<current>[^\\/]*)(?<other>.*)$/', $path, $matches);
if ($matches['current'] === '..')
$explode = explode('/', $url);
if (count($explode) > 4)
array_pop($explode);
array_pop($explode);
$url = implode('/', $explode) . '/';
else if ($matches['current'] === '.')
$explode = explode('/', $url);
if (count($explode) > 4)
array_pop($explode);
$url = implode('/', $explode) . '/';
else
$url .= $matches['current'] . '/';
$path = ltrim($matches['other'], '/');
while(!empty($matches['other']));
return rtrim($url, '/');
// 测试实例
$url = "http://jjx.com/hello/world/this/is";
$path = '../../hello.html';
echo parseUrl($url, $path);
以上是关于php 模拟返回url上一层目录的主要内容,如果未能解决你的问题,请参考以下文章