【问题标题】:How to combine the common substring from 2 strings in PHP如何在 PHP 中组合来自 2 个字符串的公共子字符串
【发布时间】:2018-03-08 15:09:26
【问题描述】:

我有两个字符串

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

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

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

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

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

以及如何表明从 $new_string 中删除了 c 和 d?

非常感谢

【问题讨论】:

标签: php duplicates substring string-comparison


【解决方案1】:

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

$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

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-27
    • 2021-09-17
    • 2023-03-29
    • 1970-01-01
    • 2021-05-19
    相关资源
    最近更新 更多