I'm trying to get docblocks preceding certain function calls in a PHP file. The difference to the usual approach is that I'm not trying to parse docblocks of function definitions.
Example file:
<?php
$data = get_data($id);
if ( empty( $data->random ) ) {
/**
* Brief description
*
* @since 1.0
* @param int $var Variable
*/
do_function( 'identifier', $var );
exit;
}
// random comment
$status = get_random_function($post);
?>
do_function
does appear on various places in various files I'm going to parse. What I'm trying to get and parse is the preceding docblock including the function call.
A Reflection class is not an option as the files don't include classes, so I'm stuck with the following RegExp which returns an empty array:
preg_match_all('/(\/\*.+\*\/)[\s]{0,}do_function/m', $filecontent_as_string, $results);
What am I doing wrong here? Thanks!
Check out Tokenizer or Reflection for this case. You may also see file in which you could use to match those certain lines of comments and have it return an array of lines.
If you desire a regular expression in this case, this should do what you want.
See a demo in action here
Regular expression: