在php中打印两个字符串之间的差异


print difference between 2 strings in php

我有一个输出流,它保存在一个名为$output1的字符串中,并显示给用户。现在,如果我有另一个名为$output2的流,它与$output1相同,再加上几行,我如何才能只输出$output2中不在$outputl中的部分。

例如:

$output1="this cat is";  
$output2="this cat is mine";  

我想输出:

这只猫是我的

有两种方法可以做到这一点-

1) <?php echo $output2; ?>-你会输出这只猫是我的。

2) <?php echo $output1.str_replace($output1, "", $output2); ?>

我建议你用第一个例子。

无论如何,请描述更多你想要实现的目标。

目前,您正在指定两个几乎相同的变量。您只需指定一个变量,即可使用该变量。

这和类似

你有两张纸,一张写着"这只猫是",另一张写了"这只猫咪是我的"。你会从第二张纸上剪下"这只猫是",只留下"我的"。所以你用胶水把第一张纸和"我的"粘在一起你浪费了时间,使事情变得复杂。

如果你只想得到"我的",那么使用-str_replace($output1, "", $output2);

$output1="this cat is";  
$output2="this cat is mine";
$min=(strlen($output1)<strlen($output2)) ? $output1 : $output2 ;
$max=(strlen($output1)>strlen($output2)) ? $output1 : $output2 ;
$pos= strpos($max,$min);
//if you want "this cat is mine", maxima string
if($pos!==FALSE) echo $max;
//if you want " mine", part of the string only in the maxima one.
if($pos !==FALSE) echo substr($max,strlen($min));

编辑:

//if you want "this cat is" from shortest string and " mine" from the other one.
if($pos !==FALSE) echo $min.substr($max,strlen($min));