通過我的機器上的代碼只是跑,這樣便解決了招
$stringA = "1, 2, 3, 4, 5";
$stringB = "1, 2, 4, 5, 6";
$stringA = explode(',', $stringA);
$stringB = explode(',', $stringB);
$Difference_1 = array_diff($stringA, $stringB); // Check string A Against String B
$Difference_2 = array_diff($stringB, $stringA); // Check String B against String A
$Difference = array_merge($Difference_1, $Difference_2); // Merge the two difference arrays together
$Difference = implode(',', $Difference); // Convert to a string
echo "The Difference Between The Two Is: ".$Difference; // Echo the difference
更新
function Differences ($Arg1, $Arg2){
$Arg1 = explode (',', $Arg1);
$Arg2 = explode (',', $Arg2);
$Difference_1 = array_diff($Arg1, $Arg2);
$Difference_2 = array_diff($Arg2, $Arg1);
$Diff = array_merge($Difference_1, $Difference_2);
$Difference = implode(',', $Diff);
return "The Difference Between The Two Is: ".$Difference;
}
$stringA = "1, 2, 3, 4, 5";
$stringB = "1, 2, 4, 5, 6";
// Call By:
echo Differences($stringA, $stringB);
對不起,我與更新的延遲。我已經習慣了PHPStorm作爲一個新的腳本編輯器。
是總是5個數字的字符串? –
嘗試用php array_diff函數。將這兩個字符串分解爲數組,然後獲得差異,並再次使用implode函數將數組轉換爲字符串。 – Neo
沒有字符串可以在任何時候有更多或更少的值 – Bobster4300