【发布时间】:2014-09-09 02:35:29
【问题描述】:
所以我需要将字符串与数组进行比较(字符串作为干草堆,数组作为针),并从字符串中获取在数组中重复的元素。为此,我采用了一个示例函数,在 substr_count 函数中使用数组作为指针。
$animals = array('cat','dog','bird');
$toString = implode(' ', $animals);
$data = array('a');
function substr_count_array($haystack, $needle){
$initial = 0;
foreach ($needle as $substring) {
$initial += substr_count($haystack, $substring);
}
return $initial;
}
echo substr_count_array($toString, $data);
问题是,如果我搜索诸如 'a' 之类的字符,它会通过检查并验证为合法值,因为包含 'a'在第一个元素内。所以上面的输出1。我认为这是由于foreach() 造成的,但我该如何绕过呢?我想搜索整个字符串匹配,而不是部分匹配。
【问题讨论】:
-
我需要匹配整个单词作为一个元素。