PHP如何比较两个变量之间的字符串并只显示唯一的字符串?

Ari*_*ego -3 php string-comparison

注意:在SO中有很多方法可以使用数组键,在相同变量的值/单词之间,两个不同的数组之间,但我在互联网上看不到字符串之间的任何内容而不是两个不同变量的数组.

<?php
$a = 'this car is very beautiful and is the fast';
$b = 'this red car is very beautiful and is the fast that others';
var_export($unique_words = show_unique_strings($a, $b));
//expected output(painted on the screen): red that others
?>
Run Code Online (Sandbox Code Playgroud)

Kar*_*sad 5

正如Siphalor所说,这是一个实现

$a = 'this car is very beautiful and is the fast';
$b = 'this red car is very beautiful and is the fast that others';
echo $unique_words = show_unique_strings($a, $b);
//expected output(painted on the screen): red that others

function show_unique_strings($a, $b) {
  $aArray = explode(" ",$a);
  $bArray = explode(" ",$b);
  $intersect = array_intersect($aArray, $bArray);
  return implode(" ", array_merge(array_diff($aArray, $intersect), array_diff($bArray, $intersect)));
}
Run Code Online (Sandbox Code Playgroud)