在PHP中查找字符串中空格和下划线的正确排列

时间:2021-12-23 16:51:41

I have to parse a XFERLOG log file of all the files being written to disk, and process the said files with an external script. The issue with XFERLOG is that it replaces all spaces with underscores, while the filename on disk remains unchanged (as it should be).

我必须解析写入磁盘的所有文件的XFERLOG日志文件,并使用外部脚本处理所述文件。 XFERLOG的问题在于它用下划线替换所有空格,而磁盘上的文件名保持不变(应该是这样)。

If the original filename has a mix of spaces and underscores, this situation makes it difficult to determine the actual filename on disk, so one would have to loop through all the permutations of spaces and underscores, check each permutation again the filesystem to see if it exists.

如果原始文件名混合了空格和下划线,这种情况使得很难确定磁盘上的实际文件名,因此必须循环遍历空格和下划线的所有排列,再次检查文件系统的每个排列以查看它是否存在存在。

So lets say the logfile reads this:

所以让我们说日志文件读取:

/path/to/file/OCD_Nightmare_-_[stuff_here_2].txt

The actual file on disk looks like this:

磁盘上的实际文件如下所示:

/path/to/file/OCD Nightmare - [stuff_here 2].txt

There is 2^5 permutations here. What would be the best course of action to find the "right" string?

这里有2 ^ 5个排列。找到“正确”字符串的最佳做法是什么?

1 个解决方案

#1


1  

Possibly use str_replace for this:

可能使用str_replace:

if(str_replace('_', ' ', $filename) == str_replace('_', ' ', $logfilename))
{
    //Yay, a match!
}

Note: As mentioned in a comment below, if your filesystem has /path/to/file/OCD_Nightmare_-_[stuff_here_2].txt and /path/to/file/OCD_Nightmare -_[stuff here_2].txt, they will both match the log entry of /path/to/file/OCD Nightmare - [stuff_here 2].txt, possibly resulting in unwanted behavior. I believe this may be a very unlikely situation, but still worth noting.

注意:如下面的评论所述,如果你的文件系统有/path/to/file/OCD_Nightmare_-_[stuff_here_2].txt和/ path / to / file / OCD_Nightmare -_ [stuff here_2] .txt,它们都会匹配/ path / to / file / OCD Nightmare - [stuff_here 2] .txt的日志条目,可能导致不必要的行为。我相信这可能是一个非常不可能的情况,但仍值得注意。

#1


1  

Possibly use str_replace for this:

可能使用str_replace:

if(str_replace('_', ' ', $filename) == str_replace('_', ' ', $logfilename))
{
    //Yay, a match!
}

Note: As mentioned in a comment below, if your filesystem has /path/to/file/OCD_Nightmare_-_[stuff_here_2].txt and /path/to/file/OCD_Nightmare -_[stuff here_2].txt, they will both match the log entry of /path/to/file/OCD Nightmare - [stuff_here 2].txt, possibly resulting in unwanted behavior. I believe this may be a very unlikely situation, but still worth noting.

注意:如下面的评论所述,如果你的文件系统有/path/to/file/OCD_Nightmare_-_[stuff_here_2].txt和/ path / to / file / OCD_Nightmare -_ [stuff here_2] .txt,它们都会匹配/ path / to / file / OCD Nightmare - [stuff_here 2] .txt的日志条目,可能导致不必要的行为。我相信这可能是一个非常不可能的情况,但仍值得注意。