[英]PHP Output Words Not In String
我有兩個字符串:
$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分隔符,最后一個單詞在兩個字符串中都沒有出現。
嘗試:
$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
)
更多信息:
將字符串分解為數組,然后查看array_diff函數。
您可以使用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"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.