简体   繁体   中英

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 = explode(',', $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:

将字符串分解为数组,然后查看array_diff函数。

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM