如何在PHP中组合来自2个字符串的公共子字符串

时间:2018-03-08 15:09:26

标签: php duplicates substring string-comparison

我有2个字符串

$old_string = "a,b,c,d,";

$ new_string,通过post方法从表单中获取。

$new_string = "c,d,e,f,";

从上面的2个字符串中获取公共子字符串的最佳方法是什么?

   $common_string ="a,b,c,d,e,f,";

如何证明c和d已从$ new_string中移除?

非常感谢

1 个答案:

答案 0 :(得分:1)

您可以使用explode()函数将这些函数转换为数组,然后使用union

$old_string = "a,b,c,d,";
$oldArray =  explode($old_string, ',');
$new_string = "c,d,e,f,";
$newArray = explode($new_string, ',');

$result =  array_unique(array_merge($oldArray, $newArray));

print_r($result); //it is unique and merged

希望这有帮助