你如何提取根目录?

发布于 2024-10-07 02:06:32 字数 1471 浏览 5 评论 0原文

我还在学习php。

'SCRIPT_FILENAME' => string 'D:/Project Storage/wnmp/www/folder/index.php' (length=45)
'SCRIPT_NAME'     => string '/folder/index.php' (length=18)
'DOCUMENT_URI'    => string '/folder/index.php' (length=18)
'PHP_SELF'        => string '/folder/index.php' (length=18)
'REQUEST_URI'     => string '/folder/helloworld/helloworldtwo/etc' (length=15)

如您所见,我只想获取 helloworld/helloworldtwo/etc

任何想法提取文件夹?那么它将是 helloworld/helloworldtwo/etc

我的想法是定义我的文件夹,例如 $root = 'folder'。如果匹配的话我就提取它,但问题出在哪里?

第二个想法是从 php_self 或上面的任何内容获取第一个从 /first/second.php , 但我又不知道最好的方法。

另一个问题是当我们前面有两个文件夹时?顺便说一句,感谢所有的重播,我仍在阅读 php.net、测试和尝试。

'SCRIPT_FILENAME' => string 'D:/Project Storage/wnmp/www/folder/index.php' (length=45)
'SCRIPT_NAME'     => string '/folder/folder2/index.php' (length=18)
'DOCUMENT_URI'    => string '/folder/folder2/index.php' (length=18)
'PHP_SELF'        => string '/folder/folder2/index.php' (length=18)
'REQUEST_URI'     => string '/folder/folder2/helloworld/helloworldtwo/etc' (length=15)

问题仍然是一样的,我怎样才能以正确的方式获得 helloworld/hellowrodltwo/etc 。

编辑* 非常感谢大家,我已经制定了解决方案

$str = 'folder/folder/helloworld/helloworldtwo/etc';
$folder = 'folder/folder';
$q = str_replace($folder, NULL, $str);
echo $q;

,但如果有任何/替代方案或更好的方法,请这样做。

再次感谢。

I am still learning php.

'SCRIPT_FILENAME' => string 'D:/Project Storage/wnmp/www/folder/index.php' (length=45)
'SCRIPT_NAME'     => string '/folder/index.php' (length=18)
'DOCUMENT_URI'    => string '/folder/index.php' (length=18)
'PHP_SELF'        => string '/folder/index.php' (length=18)
'REQUEST_URI'     => string '/folder/helloworld/helloworldtwo/etc' (length=15)

as you can see i just want to get the helloworld/helloworldtwo/etc

any idea to extract the folder ? so it will be helloworld/helloworldtwo/etc ?

what im thinking is im defining my folder like $root = 'folder'. then i extract it if it match, but the problem is with what ?

the second idea is to get from php_self or anything above to get the first from /first/second.php,
but again i dont know the best way to do it.

and another problem is when we have like two folders in the front ? btw thanks for all of the replay, im still doing some read php.net, test, and try.

'SCRIPT_FILENAME' => string 'D:/Project Storage/wnmp/www/folder/index.php' (length=45)
'SCRIPT_NAME'     => string '/folder/folder2/index.php' (length=18)
'DOCUMENT_URI'    => string '/folder/folder2/index.php' (length=18)
'PHP_SELF'        => string '/folder/folder2/index.php' (length=18)
'REQUEST_URI'     => string '/folder/folder2/helloworld/helloworldtwo/etc' (length=15)

the question is still the same how can i get the helloworld/hellowrodltwo/etc the right way.

edit*
guys thanks a lot i have made a solution

$str = 'folder/folder/helloworld/helloworldtwo/etc';
$folder = 'folder/folder';
$q = str_replace($folder, NULL, $str);
echo $q;

but if there is anything / alternative or a better way to do it please do.

Thanks again.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧竹 2024-10-14 02:06:32

您可以在PHP中使用explode函数

$str = 'folder/helloworld/helloworldtwo/etc';

print_r(explode('/', $str, 2));

输出将是:

Array
(
    [0] => folder
    [1] => helloworld/helloworldtwo/etc
)

如果您有多个文件夹/,则可以使用'folder/'作为分隔符并且不施加限制

$str = 'folder/folder/helloworld/helloworldtwo/etc';

print_r(explode('folder/', $str));

输出将是:

array (
  0 => '',
  1 => '',
  2 => 'helloworld/helloworldtwo/etc',
)

那么您可以使用implode函数将其重新加入到字符串中

$returnValue = implode('', array (
  0 => '',
  1 => '',
  2 => 'helloworld/helloworldtwo/etc',
));

加入这两个函数,您可以从 url 中删除所需的文件夹数量,并将干净的 url 放在字符串末尾

You can use the explode function in PHP

$str = 'folder/helloworld/helloworldtwo/etc';

print_r(explode('/', $str, 2));

The output will be:

Array
(
    [0] => folder
    [1] => helloworld/helloworldtwo/etc
)

If you have multiple folder/ you can do use 'folder/' as the delimiter and don't impose a limit

$str = 'folder/folder/helloworld/helloworldtwo/etc';

print_r(explode('folder/', $str));

The output will be:

array (
  0 => '',
  1 => '',
  2 => 'helloworld/helloworldtwo/etc',
)

then you can use the implode function to join it back into a string

$returnValue = implode('', array (
  0 => '',
  1 => '',
  2 => 'helloworld/helloworldtwo/etc',
));

Joining this 2 functions you can remove how many folders you want from the url and have the clean url at the end in a string

扬花落满肩 2024-10-14 02:06:32

如果您知道您想要删除的始终是 "/folder/" ,那么您可以使用类似以下内容的内容:

$extracted = str_replace("/folder/","",$_SERVER['REQUEST_URI'],1);

What this dost it Replaces 所有出现的 /folder/ 为和空字符串。这会导致 /folder/helloworld/folder/helloworld2 等 url 出现问题

If you know that it will always be "/folder/" that you want removed then you can use something like:

$extracted = str_replace("/folder/","",$_SERVER['REQUEST_URI'],1);

What this doest it replaces all occurences of /folder/ with and empty string. This will give problems with urls such as /folder/helloworld/folder/helloworld2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文