2016-07-29 19 views
0

Wie zählen preg_match Übereinstimmungen in Smarty? Warum funktioniert das Folgende nicht?Zählen preg_match in smarty

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

{$match|@count} 
+0

Was nicht funktioniert? Was ist der Inhalt von '$ code'? Was bekommst du? Was erwartest du? – Toto

+0

$ code = „[PGN1] 1. D4 Sf6 2. c4 g6 [/ PGN1] [PGN2] 1. D4 Sf6 2. c4 g6 [/ PGN2] [PGN3] 1. D4 Sf6 2. c4 g6 [/ PGN3] [PGN4] 1. d4 Sf6 2. c4 g6 [/ PGN4] [PGN5] 1. d4 Sf6 2. c4 g6 [/ PGN5] [PGN6] 1. d4 Sf6 2. c4 g6 [/ PGN6] [PGN7] 1. d4 Sf6 2. c4 g6 [/ PGN7] " So würde ich die Antwort 7 erwarten. Aber irgendwie bekomme ich" 1 2 " –

Antwort

0

Soweit ich Ihre Bedürfnisse zu verstehen, wird die folgende Arbeit, nur die Regex machen nicht gierig:

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

Danke, aber {$ match | @count} zeigt 0 an :( –

+0

@EmanuelSchiendorfer: Sorry, ich habe einen Tippfehler gemacht, benutze 'preg_match_all'. Siehe meine Bearbeitung. – Toto

+0

Danke! Jetzt funktioniert es :) –