繁体   English   中英

使用strpos()时遇到问题

[英]Having trouble using strpos()

此函数会产生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';
    }
if (strpos(" ", $userString, $value) !== false)

必须成为

if (strpos($userString, $value) !== false)

使用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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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