计算 strpos() 出现次数

Count strpos() Occurences

如何统计关键字的出现次数?目前我有这个

$filesfound = false;
foreach ($arr as $file)
$filesfound = true;
...
echo $vouchers = ($filesfound === true) && (strpos(implode('/', $file), 'Voucher') !== false) ? '<div>+ 2 Files Available</div>' : '';

我正在尝试获取“+2 个可用文件”以实际解释找到的字符串。我怎样才能做到这一点?多个函数没有给出我期望的结果。在这种情况下,我期望正确读取 2

谢谢!!


解决方案

$filesfound = false;
foreach ($arr as $file) {
   // If occurrences were found. Returns 0 for each match, so we add + 1
   if (strpos(implode('/', $file), 'Voucher') === 0) { 
      $filesfound = true;
      $count = $count + 1; 
   } 
}
echo $count;

要计算字符串中某个文本的出现次数,您可以使用:

$text = 'This is a test';
// find how many "is" are within our $text variable:
echo substr_count($text, 'is'); // this will return 2, since there are 2 of "is" in our $text