首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >计数strpos()发生

计数strpos()发生
EN

Stack Overflow用户
提问于 2022-01-28 21:46:57
回答 1查看 212关注 0票数 0

如何计算已找到的关键字出现的次数?目前我有这个

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

我试图获得"+2文件可用“,以实际帐户字符串发生的发现。我该怎么做?多个函数没有给出我期望的结果。在这种情况下,我期望2的正确阅读

谢谢!!

溶液

代码语言:javascript
复制
$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;
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-28 22:11:56

若要计数字符串中出现的文本,可以使用:

代码语言:javascript
复制
$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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70900646

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档