你如何提取根目录?
我还在学习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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在PHP中使用explode函数
输出将是:
如果您有多个文件夹/,则可以使用'folder/'作为分隔符并且不施加限制
输出将是:
那么您可以使用implode函数将其重新加入到字符串中
加入这两个函数,您可以从 url 中删除所需的文件夹数量,并将干净的 url 放在字符串末尾
You can use the explode function in PHP
The output will be:
If you have multiple folder/ you can do use 'folder/' as the delimiter and don't impose a limit
The output will be:
then you can use the implode function to join it back into a string
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
如果您知道您想要删除的始终是
"/folder/"
,那么您可以使用类似以下内容的内容: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: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