How to parse phpDoc style comment block with php?

Posted by Reveller on Stack Overflow See other posts from Stack Overflow or by Reveller
Published on 2010-05-01T08:24:42Z Indexed on 2010/05/01 8:37 UTC
Read the original article Hit count: 277

Filed under:
|
|
|
|

Please consider the following code with which I'm trying to parse only the first phpDoc style comment (noy using any other libraries) in a file (file contents put in $data variable for testing purposes):

$data = "
/**
 * @file    A lot of info about this file
 *          Could even continue on the next line
 * @author  [email protected]
 * @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('/@([a-z]+)\s+(.*?)\s*(?=$|@[a-z]+\s)/s', $data, $matches); 
$info = array_combine($matches[1], $matches[2]);
print_r($info)

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

Array (
    [file] => A lot of info about this file Could even continue on the next line
    [author] => [email protected]
    [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 altered so that only the first comment block is being parsed (in other words: parsing should stop after the first "*/" encountered?

© Stack Overflow or respective owner

Related posts about php

Related posts about parsing