$array = array(
'abacab',
'baccarat',
'bejazzle',
'barcode',
'zyx',
);
$source = "abcde";
$sourceArray = str_split($source);
foreach($array as $value) {
$matches = array_intersect($sourceArray, str_split($value));
echo $value;
if (count($matches) == 0) {
echo ' contains none of the characters ', $source, PHP_EOL;
} elseif (count($matches) == count($sourceArray)) {
echo ' contains all of the characters ', $source, PHP_EOL;
} else {
echo ' contains ', count($matches), ' of the characters ', $source, ' (', implode($matches), ')', PHP_EOL;
}
}
給
abacab contains 3 of the characters abcde (abc)
baccarat contains 3 of the characters abcde (abc)
bejazzle contains 3 of the characters abcde (abe)
barcode contains all of the characters abcde
zyx contains none of the characters abcde
嘗試爆炸的$源字符數組,然後在你的循環中使用[array_intersect()](http://www.php.net/manual/en/function.array-intersect.php) –
因此,基本上我應該調用'$ s ource_array = explode(「」,$ source);'併爲'$ value'做同樣的事情?空'分隔符'返回False。它應該是什麼? – iTurki
使用[str_split()](http://www.php.net/manual/en/function.str-split.php) –