预赛比赛计数比赛
问题描述:
我有一个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 .