Finding the correct permutation of spaces and underscores in a string, in PHP

I need to analyze the XFERLOG log file of all files written to disk and process the specified files using an external script. The problem with XFERLOG is that it replaces all spaces with underscores, and the file name on the disk remains unchanged (as it should be).

If the original file name has a combination of spaces and underscores, this situation makes it difficult to determine the actual file name on the disk, so you would have to go through all permutations of spaces and underscores, check each permutation of the file system again to see if it exists.

So let's say the log file 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 are 2 ^ 5 permutations here. What would be the best way to find the "right" string?

+4
source share
1 answer

Perhaps str_replace is used for this:

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

Note. As stated in the comment below, if your file system has /path/to/file/OCD_Nightmare_-_[stuff_here_2].txtand /path/to/file/OCD_Nightmare -_[stuff here_2].txt, they will both match the log entry /path/to/file/OCD Nightmare - [stuff_here 2].txt, which can lead to undesirable behavior. I believe that this may be a very unlikely situation, but it is still worth noting.

+1
source

Source: https://habr.com/ru/post/1538914/


All Articles