开发者

Exract text between HTML tags and count them

so lets say I have written an article with many tags like [code]this is a code[/code], and I would know how many code tags are in the article and what is the text inside.

I tried preg_matches and preg_replaces, but not开发者_如何学Pythonhing has worked so far. What would be the appropriate way to do it?


$pattern = '/\[code\](.*?)\[\/code\]/s';

preg_match_all($pattern, $code, $matches);

echo count($matches)."\n";

var_dump($matches);


This should be interesting for your:

/\[code\]([^]]+)\[\/code\]/ 

You need to use a match_all to get all the values. BTW there must be some flaw with code like this :

[code]blabla [code]bleh bleh[/code][/code]

Since regex cannot parse with multiple levels of depth. At least when the depth is unknown.

Edit

The /\[code\](.*)\[\/code\]/ can be useful too, but will not catch the inner block. The first one does match only the inner one.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜