-2

How can I read the comment block annotations in the PHP file dynamically?

Example:-

<?php
/**
* @author John Doe ([email protected])
* @copyright MIT
*/

How can I get the @author using another php file?

5
  • 1
    See stackoverflow.com/q/4702356/231316, stackoverflow.com/q/2531085/231316 and many others Commented Nov 9, 2020 at 15:45
  • @ChrisHaas Thanks, man! but my question was about parsing file DocBlock not class or method DocBlock Commented Nov 9, 2020 at 15:58
  • 1
    It might also be worth looking into this. Although that is specifically for WordPress, it is also about parsing comments at a specific location in a file using a pattern that is similar to what you are looking at, with wildcard support for the various Commented Nov 9, 2020 at 16:27
  • @ChrisHaas That exactly what I am looking for! Commented Nov 9, 2020 at 16:44
  • Does this answer your question? How is PHP used to parse comments / headers for themes and plugins? Commented Nov 9, 2020 at 21:14

1 Answer 1

1

don't think there's any "good way" to do it, unfortunately. just regex it, eg

<?php
/**
* @author John Doe ([email protected])
* @copyright MIT
*/
/**
* @author Moe ([email protected])
* @copyright MIT
*/

$php_file = __FILE__;
$content = file_get_contents($php_file);
$rex = <<<'REX'
/\n\*\s*\@author\s*(?<author>[^\n]+?)\s*?$/m
REX;
$authors = [];
if(preg_match_all($rex,$content,$matches)){
    $authors += $matches["author"];
}
var_dump($authors);

and even that is not particularly reliable, but that specific script yields

array(2) {
  [0]=>
  string(27) "John Doe ([email protected])"
  [1]=>
  string(21) "Moe ([email protected])"
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.