我有一个字符串和一个值数组,我想检查数组中的项目出现在字符串中的次数。
这是最快的方法吗?
$appearsCount = 0;
$string = "This is a string of text containing random abc def";
$items = array("abc", "def", "ghi", "etc");
foreach($items as $item)
{
$appearsCount += substr_count($string, $item);
}
echo "The item appears $appearsCount times";