开发者

PHP Output Words Not In String

I have two strings:

$string = shell_exec('reg.bat '.$arg); //returns word\0word\0word
$stringA = "$string";
$stringB = "word,other,word2,other2";
$array1 = explode('\0', $stringA);
$array2 = e开发者_如何学Pythonxplode(',', $stringB);
$result = array_diff($array1, $array2);

I can use array_diff to find the differences, but the last word shows up as not in both strings even though it is because of the \0 delimiter.


Try:

$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);

Result:

Array
(
    [3] => other2
)

More Info:

  • http://nl3.php.net/manual/en/function.array-diff.php


Explode the string to an array and have a look at the array_diff function.


You can use array_diff and 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"

Or switch the input around:

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"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜