Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8244233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:46:57+00:00 2026-06-07T21:46:57+00:00

I’ve been trying to get the comments out of a certain .php file on

  • 0

I’ve been trying to get the comments out of a certain .php file on my server, in order to parse its variables. I thought Ii found an easy way to do this, however, the function I use doesn’t return anything, even though I clearly have comments in the file.

Here are the comments I use:

/**
* @param  foo bar
* @return baz
*/

Here’s my code:

function GetComments($filename) {

    $expr = "/((?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:\/\/.*))/";

    $file = fopen($filename, "r");
    $length = filesize($filename);
    $comments = fread($file, $length);
    fclose($file);

    preg_match_all($expr, $comments, $matches);

    foreach($matches[0] as $id => $variable) {
        $comments = str_replace($variable, '', $comments);
    }

    return $comments;
}

Is there something I’m doing wrong? Because if so, I’m clearly over looking it.

Any help would be very welcome.


I found an answer:

First of all, I should have probably noted in my question that I am trying to write a system for reading plugins. These plugin files should contain a comment block at the top containing variables like the plugin’s author, website, email, etc.

So here’s what I did:

I took feeela’s example to change my function to get the comments and its variables.

I then changed its code a bit to fit my needs:

public function GetComments($filename)
{

    $docComments = array_filter(token_get_all(file_get_contents($filename)), function($entry)
    {
        return $entry[0] == T_COMMENT;
    });
    $fileDocComment = array_shift($docComments);

    $regexp = "/\@.*\:\s.*\r/";
    preg_match_all($regexp, $fileDocComment[1], $matches);

    for($i = 0; $i < sizeof($matches[0]); $i++)
    {
        $params[$i] = split(": ", $matches[0][$i]);
    }

    return($params);
}

I put the result of the code feeela gave me through a regular expression match, resulting in an array containing the parameters and their values combined.

Then I used the split function to actually give me separated parameters and values, so I could return them to the variable that called the function.

In order to get this to work properly, I needed to change the comment style I used from

/**
* @param foo bar
* @return baz
*/

to

/*
@param: foo bar
@return: baz
*/

making it a normal comment block, instead of a doc comment block.
And it also enabled me to use ‘: ‘ as a pattern for the split function.

It might be ‘not so efficient’ in the eyes of some. As feeela noted, "what if your comment style changes?". I will be the only one working on this project and writing the plugins. Therefore it will not be too hard for me too keep the comment style the same in every plugin script.

This method works perfectly for me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T21:46:59+00:00Added an answer on June 7, 2026 at 9:46 pm

    You can use token_get_all(), which "parses a given PHP source string into language tokens using the Zend engine’s lexical scanner".

    Here’s an example function I’ve used once to get the file-doc-comment from the current file:

    /**
     * Return first doc comment found in this file.
     *
     * @return string
     */
    function getFileDocBlock()
    {
        $docComments = array_filter(
            token_get_all( file_get_contents(__FILE__)), function($entry) {
                return $entry[0] == T_DOC_COMMENT;
            }
        );
        $fileDocComment = array_shift($docComments);
        return $fileDocComment[1];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
i want to parse a xhtml file and display in UITableView. what is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.