从目录路径字符串中删除根目录-PHP [英] Remove root directory from a directory path string - PHP

查看:115
本文介绍了从目录路径字符串中删除根目录-PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个变量,例如$path = "dir1/dir2/dir1/dir4/";等.等等.

I have a variable like $path = "dir1/dir2/dir1/dir4/"; etc.. etc..

我要删除第一个成员dir1/,并希望得到类似dir2/dir1/dir4/的结果.

I want to remove the first member dir1/ and want result like dir2/dir1/dir4/.

我认为可以通过explode('/', $path)将变量设置为数组来实现.如何删除第一个成员vrom数组并将该数组重构为文本变量?

I think it is possible by making the variable an array by explode('/', $path). How can I remove the first member vrom array and reconstruct that array into a text variable??

如何用PHP实现呢?

推荐答案

根据您更新的问题

仅分为两部分,第二部分.如果第二个不存在,请给它NULL:

Only explode into two parts, take the second one. In case the second one does not exists, give it NULL:

list(, $result) = explode("/", $path, 2) + array( 1 => NULL);

OR

$array = explode("/", $path);
unset($array[0]);
echo $text = implode("/", $array);

这篇关于从目录路径字符串中删除根目录-PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆