预赛比赛计数比赛 [英] preg match count matches

查看:91
本文介绍了预赛比赛计数比赛的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个preg match语句,它检查匹配项,但是我想知道如何计算匹配项.任何建议表示赞赏.

I have a preg match statement, and it checks for matches, but I was wondering how you can count the matches. Any advice appreciated.

$message='[tag] [tag]';
preg_match('/\[tag]\b/i',$message);

例如,此消息字符串的计数应导致2个匹配项

for example a count of this message string should lead to 2 matches

推荐答案

$message='[tag] [tag]';
echo preg_match_all('/\\[tag\\](?>\\s|$)/i', $message, $matches);

给出2.请注意,您不能使用\b,因为单词边界位于]之前,而不是之后.

gives 2. Note you cannot use \b because the word boundary is before the ], not after.

请参见 preg_match_all .

这篇关于预赛比赛计数比赛的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆