Skip to content
Advertisement

PHP – Best way to get meta data from comments

PHP has a function called get_meta_tags which can read meta tags of HTML files. However, as far as I know there is no standard way to define meta tags for PHP files. The de facto solution seems to be to add comment to the top of the file like so:

<?php

     # Author: Ood
     # Description: Hello World

?>

Is there any way to read these “meta tags” with PHP similar to the way get_meta_tags works using the default PHP library? Preferably without parsing the entire file with file_get_contents followed by a regex for best performance. If not, maybe someone knows of a better solution to add meta data capabilities to PHP files. Thanks in advance!

Advertisement

Answer

In our project we are happy with the standard JavaDoc that was adopted by PHPDoc using the @field syntax as you might know it from any PHP function or class definition. This is pretty fine readable using the PHPDocumenter.

In our adoption we use the very first multi-line comment, ie anyting between /** and closing tag */, using the JavaDoc style to describe all the details about the current script.

So to adopt your example in our project we would have following syntax:

<?php
/**
* @author Ood
* @desc  Hello World
*/

Of course you may end up with your custom function reading the beginning of the php file parsing just the very first multi-line comment to get the script description aka meta tags.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement