PHP 只获取完整路径的一部分

Adr*_*ian 4 php string path

我想知道如何只减去完整路径的一部分:

我得到当前文件夹的完整路径:

$dbc_root = getcwd(); // That will return let's say "/home/USER/public_html/test2"
Run Code Online (Sandbox Code Playgroud)

我只想选择“/public_html/test2”

我该怎么做?谢谢!

And*_*eas 5

我认为你应该检查路径相关的方法:

pathinfo() - Returns information about a file path
dirname() - Returns directory name component of path
basename() - Returns filename component of path
Run Code Online (Sandbox Code Playgroud)

您应该能够找到其中之一的解决方案。


Eri*_*rik 1

好吧,如果您知道要丢弃的路径部分是什么,您可以简单地执行 str_replace:

$dbc_root = str_replace('/home/USER/', '', $dbc_root);
Run Code Online (Sandbox Code Playgroud)