How to parse a block of comments PHP-style PHP?

Please consider the following code, with which I try to analyze only the first comment on the phpDoc style (without using any other libraries) in the file (the contents of the file are placed in the $ data variable for testing purposes):

$data = " /** * @file A lot of info about this file * Could even continue on the next line * @author me@example.com * @version 2010-05-01 * @todo do stuff... */ /** * Comment bij functie bar() * @param Array met dingen */ function bar($baz) { echo $baz; } "; $data = trim(preg_replace('/\r?\n *\* */', ' ', $data)); preg_match_all('/@([az]+)\s+(.*?)\s*(?=$|@[az]+\s)/s', $data, $matches); $info = array_combine($matches[1], $matches[2]); print_r($info) 

This almost works, except for the fact that everything after @todo (including the block and comment code bar() ) is considered the value of @todo :

 Array ( [file] => A lot of info about this file Could even continue on the next line [author] => me@example.com [version] => 2010-05-01 [todo] => do stuff... / /** Comment bij functie bar() [param] => Array met dingen / function bar() { echo ; } ) 

How does my code need to be changed so that only the first block of comments is processed (in other words: should parsing stop after the first "* /" met?

+4
source share
1 answer

Writing a parser using PCRE will lead you to problems. I suggest relying on tokenizer or reflection first. Then it is safer to actually implement the parser for the doc block, which can handle all the situations supported by the phpdoc format (which is all the libs to do too).

+6
source

Source: https://habr.com/ru/post/1308524/


All Articles