2 回答
TA贡献2003条经验 获得超2个赞
这是我的方法。我还没有测试它是否比@Adam 的更快,但我认为它看起来更干净。
$dir = $_SERVER['SCRIPT_FILENAME'];
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$intersection = implode('/',
call_user_func_array('array_intersect',
array_map(function($a) {
return explode('/', $a);
},
[$dir, $uri])
)
);
$uri_result = substr($request_uri, strlen($intersection));
TA贡献1963条经验 获得超6个赞
所以我想出了一个答案:
$dir_a = array_filter(explode('/', __DIR__));
$url_a = array_filter(explode('/', $uri));
$base_public_folder = '';
foreach ($url_a as $url_key => $url_f) {
foreach ($dir_a as $dir_key => $dir_f ) {
$next_url_f = !empty($url_a[($url_key + 1)]) ? $url_a[($url_key + 1)] : '';
$next_dir_f = !empty($dir_a[($dir_key + 1)]) ? $dir_a[($dir_key + 1)] : '';
if (
$url_f == $dir_f &&
($next_dir_f == '' || $next_url_f == $next_dir_f)
) {
$base_public_folder .= "/".$url_f;
break;
}
}
}
echo $base_public_folder;
// outputs '/my_app'
- 2 回答
- 0 关注
- 250 浏览
添加回答
举报