开发者

PHP regex to identify multiple tokens

I need to create a regex in PHP that will identify the following tokens in a section of text:

[video:1]
[video:2]

The code I'm using is

preg_match("/\[video:[12]{1}\]/", $text, $matches);

with the idea that I can have one character of either 1 or 2 after '[video:' and then ']'. The problem I'm having is that it won't identify the second token if there are two of them in the text. It will identify either one if it is the first one, but not the second one if both are in the text.

How can I identify both tokens in my section of text?

Thank开发者_Go百科s.


You need to use preg_match_all() to to a global search and capture.

Also be sure to properly escape regular expression syntax when you need to match literal characters.

/\[video:[12]\]/

Note: I also removed the quantifier as it is unnecessary since you are just matching one 1 or 2.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜