preg_split 字符串到多维数组
你好,只是在寻找一些帮助,因为我陷入了困境,
我有两个字符串:
C:\Users\Bob\My Documents
/Users/Bob/Documents
通过它
preg_split('/(?<=[\/\\\])(?![\/\\\])/', $string)
字符串
Array
(
[0] => C:\
[1] => Users\
[2] => Bob\
[3] => My Documents
)
Array
(
[0] => /
[1] => Users/
[2] => Bob/
[3] => Documents
)
返回我需要的
Array
(
[C:\] => Array
(
[Users] => Array
(
[Bob] => Array
(
[My Documents] => array()
)
)
)
)
Array
(
[/] => Array
(
[Users] => Array
(
[Bob] => Array
(
[Documents] => array()
)
)
)
)
并最终合并到
Array
(
[C:\] => Array
(
[Users] => Array
(
[Bob] => Array
(
[My Documents] => array()
)
)
)
[/] => Array
(
[Users] => Array
(
[Bob] => Array
(
[Documents] => array()
)
)
)
)
(正确合并,而不只是附加,所以如果另一个字符串以 C:\Users 开头\Dan
那么 dan 将出现在第 3 个维度上。
Hello just looking for some help as I've gotten stuck
I have two Strings:
C:\Users\Bob\My Documents
/Users/Bob/Documents
That gets put through
preg_split('/(?<=[\/\\\])(?![\/\\\])/', $string)
that returns
Array
(
[0] => C:\
[1] => Users\
[2] => Bob\
[3] => My Documents
)
Array
(
[0] => /
[1] => Users/
[2] => Bob/
[3] => Documents
)
I need
Array
(
[C:\] => Array
(
[Users] => Array
(
[Bob] => Array
(
[My Documents] => array()
)
)
)
)
Array
(
[/] => Array
(
[Users] => Array
(
[Bob] => Array
(
[Documents] => array()
)
)
)
)
And ultimately merged to
Array
(
[C:\] => Array
(
[Users] => Array
(
[Bob] => Array
(
[My Documents] => array()
)
)
)
[/] => Array
(
[Users] => Array
(
[Bob] => Array
(
[Documents] => array()
)
)
)
)
(properly merged, not just appended, so if another string started with C:\Users\Dan
Then dan would appear on the ?3rd? Dimension. array_merge_recursive()
? )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需获取
preg_split()
返回的数组并用它们构建树结构:Just take the arrays returned by
preg_split()
and build your tree structure out of them:您可能最好只使用 pathinfo()
http://uk.php。 net/manual/en/function.pathinfo.php
和 realpath()
http://uk.php.net/manual/en/function.realpath.php
我假设您正在尝试将 *nix 目录映射到 Windows 目录?
You're probably best off just using pathinfo()
http://uk.php.net/manual/en/function.pathinfo.php
And realpath()
http://uk.php.net/manual/en/function.realpath.php
I assume you're trying to map a *nix directory to a Windows one?