如何在 PHP 中使 is_dir($FoLdEr) 不区分大小写?
我需要 is_dir() 来不关心文件夹或参数是大写还是小写,或者是大写和小写的混合。
因此,如果文件夹名称是“我的文件夹”并且我运行 is_dir("mY FoLdEr")
,结果应该是 true
。
I need is_dir()
to don't care about if the folder or the argument is upper or lower case, or a mix of upper and lower.
So if the foldername is "My Folder" and I run is_dir("mY FoLdEr")
the result should be true
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这不取决于 PHP。这取决于运行 PHP 的操作系统和文件系统。 Windows 不区分大小写,但每个版本的 Unix/Linux 都区分大小写。
让我换一种说法:
is_dir()
基本上是系统调用的包装器,或者它将使用文件信息上的系统调用的结果。如果根据操作系统和文件系统的规则找到具有匹配名称的文件,这些系统调用将返回或不返回某些内容。 PHP 无法改变这一点,所以你不能只是让is_dir()
在 Linux 上不区分大小写。您能做的最好的事情就是获取目录中的文件列表,然后循环遍历它们以测试是否有任何文件与您要查找的内容匹配(不区分大小写)。
注意:您可能会获得多次匹配,例如“HOME”和“Home”都将匹配“home”。在这种情况下,这样的函数(您想要的)会做什么?
或者,您可以将所有文件名更改为小写,然后可以使用输入的小写版本来查找指定的文件。
That's not up to PHP. That's up to operating system and filesystem that PHP is running on. Windows is case insensitive but every version of Unix/Linux is case sensitive.
Let me put it another way:
is_dir()
is basically a wrapper to a system call or it will use the results of system calls on file info. Those system calls will either return something or not if, by the rules of that operating system and filesystem, a file is found with the matching name. PHP can't change this so no you can't just makeis_dir()
be case insensitive on Linux.The best you can do is get a list of files in the directory and loop through them to test to see if any match a case insensitive comparison to what you're looking for.
Note: you might get multiple hits eg "HOME" and "Home" will both match "home". What would such a function (that you want) do in this case?
Alternatively you can change all your filenames to lowercase and then you can use the lowercase version of your input to find the specified file.
您可以使用正则表达式。我不太确定 php 中的语法:
它们可能是更好的
regexp
。You can use regular expressions. I am not quite sure on the syntax in php:
They may be a better
regexp
for it.我遇到一个问题,需要验证目录路径。我不想在不同情况下混合使用类似名称的目录。即my\dir和My\Dir。我尝试了 Filip
glob('{mM}{yY} {fF}{oO} 提到的 grep 方法{lL}{dD}{eE}{rR}', GLOB_BRACE)
但我发现如果目录名称长于大约 8 个字符,它就会停止运行。所以这是我对不区分大小写的is_dir()
的解决方案;I had an issue where I needed to validate a directory path. I didn't want to have a mixture of similarly name directories in different cases. i.e. my\dir and My\Dir. I tried the grep approach mentioned by Filip
glob('{mM}{yY} {fF}{oO}{lL}{dD}{eE}{rR}', GLOB_BRACE)
but I found that if the directory name was longer than about 8 characters it would grind to a halt. So this is my solution for a case insensitiveis_dir()
;肮脏的方式可能是获取实际目录中所有目录的列表,并将它们的 strtolower()-ed 名称与所需名称进行比较
Dirty way could be getting list of all directories in actual dir and compare theirs strtolower()-ed names with desired name
这是我的解决方案:
它基本上将路径从
'/path/With/Cap-Case or not/'
转换为
'/[pP][aA][tT][hH] /[wW][iI][tT][hH]/[cC][aA][pP]-[cC][aA][sS][eE] [oO][rR] [nN][oO][tT ]/'
但由于只知道有 dir 的一些大写字母发生了变化,我认为更好的功能应该是:
here is my solution:
it basicaly does transform the path from
'/path/With/Cap-Case or not/'
to
'/[pP][aA][tT][hH]/[wW][iI][tT][hH]/[cC][aA][pP]-[cC][aA][sS][eE] [oO][rR] [nN][oO][tT]/'
But as only knowing there is dir with some cap-letters changed, I think a better function should be: