How to read comment blocks in PHP?

I'm doing some home-brewed automated documentation, since I have a codebase which is not very standard in its layout, and I was wondering what the best way was to read a PHP file and grab the contents of a comment block. The only way I can think to do it is to open the file and read it line-by-line, but thought that maybe there was some built-in magic that would parse the document for me, similar to the Reflection functions.

The basic layout of each file is like this:

<?php // $Id$ /** * Here is this script's documentation, with information in pseudo-javadoc * type tags and whatnot. * * @attr something some information about something * @attr etc etc etc */ // rest of the code goes here.

It's important to note that these files don't have any functions or classes defined in them. The comments relate to the script as a whole.

-------------Problems Reply------------

Check out Tokenizer.

To get all the comments in a file named test.php you'd do:

$tokens = token_get_all(file_get_contents("test.php"));
$comments = array();
foreach($tokens as $token) {
if($token[0] == T_COMMENT || $token[0] == T_DOC_COMMENT) {
$comments[] = $token[1];
}
}
print_r($comments);

Have a look at the Reflection API that comes with PHP5, more specifically getDocComment():

PHP 5 comes with a complete reflection API that adds the ability to reverse-engineer classes, interfaces, functions and methods as well as extensions. Additionally, the reflection API also offers ways of retrieving doc comments for functions, classes and methods.

Also, depending on the size of your codebase, you might work less by modifying your comments to fit the phpDocumentor syntax, which is already seems pretty close to.

Category:php Views:1 Time:2009-04-02

Related post

  • Where to put the doxygen comment blocks for an internal library - in H or in CPP files? 2008-12-10

    The common sense tells that the Doxygen comment blocks have to be put in the header files where the classes, structs, enums, functions, declarations are. I agree that this is a sound argument for a libraries that are mean to be distributed without it

  • When does the comment block with schema information for the model get updated by rails? 2009-01-26

    In some Rails model definitions, there is a comment block at the top that contains the schema information. # == Schema Information # Schema version: 20090122060318 # # Table name: table_name # # id :integer(4) not null, primary key ... When does this

  • TextMate source.java comment block folding 2009-03-05

    How do I modify the java language definition bundle foldingStartMarker and foldingStopMarker entries to allow for folding of these types of comment blocks? This is the comment style: /** * This is a comment... * Yet another comment... */ I've tried t

  • C# Regex to get the comments block out of pl/sql code 2009-03-17

    i want to extract the comments out of a string as a block. e.g. I have a PL/SQL code as: --comment1 select * from t_table; --i want comment 2; /*i want comment 3 */ --i want comment 4 OPEN data_cur; Here, i want all the single line and multiline comm

  • Commenting blocks in Eclipse on Mac 2009-04-12

    How do I comment blocks of code in Eclipse on a Mac? --------------Solutions------------- Command (the clover/apple key next to the space bar) forward slash (/) Apple + Shift + / will toggle a block of selected code using /* */ Apple + / will toggle

  • PHP/C/C++ - How does your initial comment block look in your programs? 2009-05-28

    I am on a large project, and have not really made any comment 'headers' in my program yet. I just started using SVN for the project the other day. I want to add the SVN tag $id$, but have not decided on any standard commenting. So, my question to eve

  • Comment blocks around JSON responses 2009-06-19

    I've noticed that some web applications return AJAX responses with JSON data embedded within a comment block. For example, this would be a sample response: /*{ "firstName": "John", "lastName": "Smith", "address": { "streetAddress": "21 2nd Street", "

  • How can I automatically prepend comment blocks to SQL Server stored procedures? 2009-08-05

    In my organization, the standard is to comment all stored procedures with a comment block that looks like this: /*-- ============================================= -- Created by: Chris McCall -- Created date: 08.05.2009 -- Purpose: Inserts new setting

  • Is there a way to continue doxygen lists across multiple comment blocks? 2010-01-08

    Basically, I'm trying to do document code along the lines of: //Description of Step 1 DoStep1_1(); DoStep1_2(); ... //Description of Step 2 DoStep2_1(); DoStep2_2(); I want the two comment blocks to turn into an ordered list in the Doxygen output. I'

  • PHP: Netbeans Macro command for /* */ comment block? 2010-01-25

    I am using Netbeans 6.8 and trying to create a custom PHP comment code block macro the /* */ style and not the usual double slash. So far with googling and asking in PHPUGPH, I got this macro code (tools->options->editor->macros): copy-to-cl

  • Is it possible to change the way Xcode indents comment blocks? 2010-02-07

    By default, Xcode automatically indents multiple lines of code within C-style comment blocks by one space: /* this is a comment block line 1 line 2 */ Is it possible to modify this behaviour? I would prefer to have no indentation within comment block

  • Any software to auto generate doxygen comment blocks? 2010-03-26

    i'm developing a quite big C++ software and now (better late than neve :)) i've decided to document it in the doxy standards. There are plenty of classes, methods, functions, macros and so on therefore i'm searching for a software that would scan my

  • How to parse a phpDoc style comment block with PHP? 2010-05-01

    Please consider the following code with which I'm trying to parse only the first phpDoc style comment (not using any other libraries) in a file (file contents put in $data variable for testing purposes): $data = " /** * @file A lot of info about this

  • How to update comment block in front of methods in Netbeans? 2010-10-25

    I know this is not programming related question so to speak, but since we are programmers and we might use Netbeans for PHP development. I am wondering how can I get in Netbeans to update the comment block in front of a method, after I change the par

  • In Visual Studio, is there a quick way to generate these types of comment blocks? 2010-11-24

    If anyone knows the name for these types of comments, if one exists, please modify my question. I frequently see comment blocks such as this: /********************************************** * Some Important Text Here *********************************

  • How to parse this comment block with PHP using a regular expression? 2010-11-27

    I am having a problem in the preg_match_all() function. What would be a regular expression pattern for this type of string? Consider this code: $str="* Function do Something * @param String $variable1 * @param String $variable2 * @return String"; I w

  • adding + collapsing tree lists to large comment blocks - C# code in Visual Studio 2008 2010-12-08

    I am working on C# programming assignment for college. Part of my final for the class. The prof gave us a program, that works, but is very poorly written. We have to clean up the code and add our own flair to it, but we have to do so with the origina

  • php preg_replace comment blocks 2011-03-22

    the patern is like so /* comment [comment goes here] */ /* comment please do not delete the lines below */ [I am a special line so I should not be changed ] /* comment please do not delete the line above */ I would like to remove the comment blocks,

  • How to avoid indenting comment blocks with vim cindent? 2011-03-24

    I have a comment block such as: /*++ Blah: blah Foo: foo --*/ And I'm using the following vim cindent options: set shiftwidth=2 set tabstop=2 set cindent set cino=g0,+0,(0,W2 If I select that comment block and indent it with =, vim turns it into: /*+

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.144 (s). 11 q(s)