此功能会产生6个匹配,但应该会产生2个匹配。我不确定我在这里做错了什么。
public function displayPrize() {
$testString = "The cow jumped over the moon";
$userString = "The cow";
$magicArray = (explode(" ", $testString));
foreach ($magicArray as $value) {
if (strpos(" ", $userString, $value) !== false) {
$count++;
}
}
echo $count . ' matches';
}
答案 0 :(得分:0)
if (strpos(" ", $userString, $value) !== false)
必须成为
if (strpos($userString, $value) !== false)
答案 1 :(得分:0)
使用array_intersect()的替代方式:
$testString = 'The cow jumped over the moon';
$userString = 'The cow';
$testStringArray = explode(' ', $testString);
$userStringArray = explode(' ', $userString);
$result = count( array_intersect($testStringArray, $userStringArray) ); // 2