2016-07-29 58 views
0

如何计算smarty中的preg_match匹配?为什么以下不起作用?在smarty中计数preg_match

{preg_match("/\[PGN.*](.*)\[\/PGN.*\]/", $code, $match)} 

{$match|@count} 
+0

什么是不工作? '$ code'的内容是什么?你得到了什么?你能指望什么? – Toto

+0

$代码=“[PGN1] 1。D4 NF6 2. C4 G6 [/ PGN1] [PGN2] 1。D4 NF6 2. C4 G6 [/ PGN2] [PGN3] 1。D4 NF6 2. C4 G6 [/ PGN3] [PGN4] 1。D4 NF6 2. C4 G6 [/ PGN4] [PGN5] 1。D4 NF6 2. C4 G6 [/ PGN5] [PGN6] 1。D4 NF6 2. C4 g6 [/ PGN6] [PGN7] 1. d4 Nf6 2. c4 g6 [/ PGN7]“ 所以我会期待答案7.但不知何故,我得到”1 2“ –

回答

0

据我了解您的需求,下面不工作,只是让正则表达式不贪:

preg_match_all("~\[PGN\d+](.+?)\[/PGN\d+]~", $code, $match) 
+0

谢谢,但{$ match | @count}显示0 :( –

+0

@EmanuelSchiendorfer:对不起,我犯了一个错字,使用'preg_match_all'。看我的编辑。 – Toto

+0

谢谢!现在它的工作:) –