简体   繁体   English

删除两个字符串的相等部分

[英]Remove equal part of two strings

In PHP, I have two paths on a server that both have a matching part. 在PHP中,我在服务器上有两个路径,两个路径都有匹配的部分。 I'd like to join them, but delete the part that is equal. 我想加入他们,但删除相等的部分。

EXAMPLE: 例:

Path #1: /home7/username/public_html/ dir/anotherdir/wp-content/uploads 路径#1:/ home7 / username / public_html / dir / anotherdir / wp-content / uploads

Path #2: /dir/anotherdir/wp-content/uploads /2011/09/image.jpg 路径二:/ DIR / anotherdir /可湿性粉剂内容/上传 /2011/09/image.jpg

You see the part /dir/anotherdir/wp-content/uploads is the same in both strings, but when I simply join them I would have some directories twice. 您会看到/ dir / anotherdir / wp-content / uploads部分在两个字符串中都相同,但是当我简单地加入它们时,我将有一些目录两次。

The output I need is this: /home7/username/public_html/dir/anotherdir/wp-content/uploads/2011/09/image.jpg 我需要的输出是这样的: /home7/username/public_html/dir/anotherdir/wp-content/uploads/2011/09/image.jpg

Since the dirs can change on different servers I need a dynamic solution that detects the matching part from #2 and removes it on #1 so I can trail #2 right after #1 :) 由于Dirs可以在不同的服务器上更改,因此我需要一个动态解决方案,该解决方案可以检测#2中匹配的部分并在#1上将其删除,因此我可以在#1之后跟踪#2 :)

$path1 = "/home7/username/public_html/dir/anotherdir/wp-content/uploads";
$path2 = "/dir/anotherdir/wp-content/uploads/2011/09/image.jpg";

echo $path1 . substr($path2, strpos($path2, basename($path1)) + strlen(basename($path1)));

The problem is not so generic here. 这里的问题不是那么普遍。 You should not look at the problem as matching equal parts of strings, rather you should look at it like equal directory structure. 您不应将问题视为匹配字符串的相等部分,而应将其视为相等的目录结构。 That said you need to concentrate on strings after '/'. 也就是说,您需要专注于'/'之后的字符串。 So basically you need to do string matching of directory names. 因此,基本上,您需要对目录名称进行字符串匹配。 Moreover your problem looks like that first input file name's last part of directory structure name may be common to some part (starting from first character) of second input string. 此外,您的问题看起来目录结构名称的第一个输入文件名的最后一部分可能与第二个输入字符串的某些部分(从第一个字符开始)相同。

So I will suggest to start reading the first input from end at the jumps of '/' and try to get first string matching with the first folder name in second file-path. 因此,我建议从末尾的“ /”开始读取第一个输入,并尝试在第二个文件路径中获取与第一个文件夹名称匹配的第一个字符串。 If match happens then rest of the string character from this index to last index in first file-path should be there in first part of second input string. 如果发生匹配,则从该索引到第一个文件路径中最后一个索引的字符串字符的其余部分应位于第二个输入字符串的第一部分。 If this condition fails the repeat the process of finding the first directory name in second string matching with a directory name in first file-name for next index. 如果此条件失败,请重复执行以下过程:在第二个字符串中找到与下一个索引的第一个文件名中的目录名匹配的第一个目录名。

This code can help you: 此代码可以帮助您:

$str1 = $argv[1];
$str2 = $argv[2];

//clean
$str1 = trim(str_replace("//", "/", $str1), "/");
$str2 = trim(str_replace("//", "/", $str2), "/");

$paths1 = explode("/", $str1);
$paths2 = explode("/", $str2);

$j = 0;
$found = false;
$output = '';

for ($i=0; $i<count($paths1); $i++) {
        $item1 = $paths1[$i];
        $item2 = $paths2[$j];

        if ($item1 == $item2) {
                if (!$found)
                        $found = $i; //first point
                $j++;
        } else if ($found) {
                //we have found a subdir so remove
                $output = "/".implode("/", array_slice($paths1, 0, $i))
                                ."/".implode("/", array_slice($paths2, $j));
                $found = false;
                break;
        }
}

//final checking
if ($found) {
        $output = "/".implode("/", $paths1)
                                ."/".implode("/", array_slice($paths2, $j));
}

print "FOUND?: ".(!empty($output)?$output:'No')."\n";

Will detect the equal substrings and will cut the first string until that point and copy the other part from second string. 将检测相等的子字符串,并将第一个字符串剪切到该点,然后从第二个字符串复制另一部分。

This code will accept also two strings if they share "partial" substrings like: 如果它们共享“部分”子字符串,则此代码还将接受两个字符串:

/path1/path2/path3
/path2/other/file.png

will output: /path/path2/other/file.png 将输出: /path/path2/other/file.png

And will remove the "path3", but with few changes can be more strict 并会删除“ path3”,但只需很少更改即可更严格

how about using the similar_text as described in this link. 如何使用链接中所述的similar_text It returns the matching chars between two strings. 它返回两个字符串之间的匹配字符。 Once you have it, replace the first one with empty string and append the second. 有了它后,将第一个替换为空字符串,然后追加第二个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM