0

我有两个字符串:

$string = shell_exec('reg.bat '.$arg); //returns word\0word\0word
$stringA = "$string";
$stringB = "word,other,word2,other2";
$array1 = explode('\0', $stringA);
$array2 = explode(',', $stringB);
$result = array_diff($array1, $array2);

我可以使用 array_diff 来查找差异,但最后一个单词显示为不在两个字符串中,即使它是因为 \0 分隔符。

4

3 回答 3

2

尝试:

$stringA = "word,other,word2,other";
$stringB = "word,other,word2,other2";

$array1 = explode(',', $stringA);
$array2 = explode(',', $stringB);

$result = array_diff($array2, $array1);

echo '<pre>';
print_r($result);

结果:

Array
(
    [3] => other2
)

更多信息:

于 2010-08-04T14:07:32.923 回答
1

您可以使用 array_diff 和 str_word_count:

print_r(
    array_diff(
        str_word_count("word\0word\0word", 1, '0123456789'),
        str_word_count("word,other,word2,other2", 1, '0123456789'))
);
// will return an empty array, because "word" is in "word,other,word2,other2"

或者切换输入:

print_r(
    array_diff(
        str_word_count("word,other,word2,other2", 1, '0123456789'),
        str_word_count("word\0word\0word", 1, '0123456789'))
);
// will return an array containing "other", "word2", "other"
于 2010-08-04T14:19:26.767 回答
1

将字符串分解为数组并查看array_diff函数。

于 2010-08-04T14:04:58.097 回答