Read comments in php

Read file comments in PHP, NOT file content

Output: Welcome to PHP Single line comments PHP Multi Line Comments In PHP, we can comments multiple lines also. PHP Single Line Comments There are two ways to use single line comments in PHP.

Read file comments in PHP, NOT file content

I need to read the first «batch» of comment in a PHP file. An example would be:

I need to read the first comment inside another file, but how do I get the /** This is some basic file info **/ as a string?

There’s a token_get_all($code) function which can be used for this and it’s more reliable than you first might think.

Here’s some example code to get all comments out of a file (it’s untested, but should be enough to get you started):

I think you can also try this:

/** * Return first doc comment found in this file. * * @return string */ function getFileCommentBlock($file_name) < $Comments = array_filter( token_get_all(file_get_contents($file_name)), function($entry) < return $entry[0] == T_DOC_COMMENT; >); $fileComment = array_shift($Comments); return $fileComment[1]; > 
preg_match("/\/\*\*(.*?)\*\*\//", $file, $match); $info = $match[1]; 
$file_contents = '/** sd asdsa das sa das sa a ad**/'; preg_match('#/\*\*(.*)\*\*/#s', $file_contents, $matches); var_dump($matches); 

How PHP Comments Work, Comment in PHP, Multi-Line, We do this by commenting on the code we do not want to use, and while our code is Duration: 2:59

Читайте также:  Python save json files

Learn How To Comment PHP Code

How PHP Comments Work, Comment in PHP, Multi-Line

Get comments in a PHP file

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 */ 
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.

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.

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 */ 

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.

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]; > 

Try using The ReflectionClass class and The ReflectionFunction class .

This all is you need to get function parameters.

Have a look at the Comment Manager. It is a set of PHP classes for parsing DocBloc comments. It also allows validating function parameter values against information in DocBlock comments.

PHP include and require, The include (or require ) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include

PHP Comments

PHP comments can be used to describe any line of code so that other developer can understand the code easily. It can also be used to hide any code.

PHP supports single line and multi line comments. These comments are similar to C/C++ and Perl style (Unix shell style) comments.

PHP Single Line Comments

There are two ways to use single line comments in PHP.

PHP Multi Line Comments

In PHP, we can comments multiple lines also. To do so, we need to enclose all lines within /* */. Let’s see a simple example of PHP multiple line comment.

String — PHP Script — Comment/Uncomment line, $line; > // replace the lines with the toggled value $lines[$lineNumber] = $line; > // finally we write the lines to the file // I’m using

Источник

Read file comments in PHP, NOT file content

Output: Welcome to PHP Single line comments PHP Multi Line Comments In PHP, we can comments multiple lines also. PHP Single Line Comments There are two ways to use single line comments in PHP.

Read file comments in PHP, NOT file content

I need to read the first «batch» of comment in a PHP file. An example would be:

I need to read the first comment inside another file, but how do I get the /** This is some basic file info **/ as a string?

There’s a token_get_all($code) function which can be used for this and it’s more reliable than you first might think.

Here’s some example code to get all comments out of a file (it’s untested, but should be enough to get you started):

I think you can also try this:

/** * Return first doc comment found in this file. * * @return string */ function getFileCommentBlock($file_name) < $Comments = array_filter( token_get_all(file_get_contents($file_name)), function($entry) < return $entry[0] == T_DOC_COMMENT; >); $fileComment = array_shift($Comments); return $fileComment[1]; > 
preg_match("/\/\*\*(.*?)\*\*\//", $file, $match); $info = $match[1]; 
$file_contents = '/** sd asdsa das sa das sa a ad**/'; preg_match('#/\*\*(.*)\*\*/#s', $file_contents, $matches); var_dump($matches); 

How PHP Comments Work, Comment in PHP, Multi-Line, We do this by commenting on the code we do not want to use, and while our code is Duration: 2:59

Learn How To Comment PHP Code

How PHP Comments Work, Comment in PHP, Multi-Line

Get comments in a PHP file

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 */ 
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.

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.

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 */ 

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.

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]; > 

Try using The ReflectionClass class and The ReflectionFunction class .

This all is you need to get function parameters.

Have a look at the Comment Manager. It is a set of PHP classes for parsing DocBloc comments. It also allows validating function parameter values against information in DocBlock comments.

PHP include and require, The include (or require ) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include

PHP Comments

PHP comments can be used to describe any line of code so that other developer can understand the code easily. It can also be used to hide any code.

PHP supports single line and multi line comments. These comments are similar to C/C++ and Perl style (Unix shell style) comments.

PHP Single Line Comments

There are two ways to use single line comments in PHP.

PHP Multi Line Comments

In PHP, we can comments multiple lines also. To do so, we need to enclose all lines within /* */. Let’s see a simple example of PHP multiple line comment.

String — PHP Script — Comment/Uncomment line, $line; > // replace the lines with the toggled value $lines[$lineNumber] = $line; > // finally we write the lines to the file // I’m using

Источник

Получить все комментарии из файла

Получить все комментарии из php-файла. Используется token_get_all .

 class stdinComments { private $txt; public function run() { $this->getText(); $this->getComments(); $this->echoComments(); } private function getText() { $handle = fopen('php://stdin', 'r'); while (!feof($handle)) { $line = fgets($handle); if (empty($line)) continue; $this->txt .= $line; } fclose($handle); } private static $comment = array( T_COMMENT, // All comments since PHP5 // T_ML_COMMENT, // Multiline comments PHP4 only T_DOC_COMMENT // PHPDoc comments ); private $comments = array(); private function getComments() { $tokens = token_get_all($this->txt); foreach($tokens as $token) { if(!in_array($token[0], self::$comment)) continue; $this->comments[] = $token[1]; } } private function echoComments() { foreach($this->comments as $comment) { echo $comment.PHP_EOL; } } } $app = new stdinComments(); $app->run();
cat index.php | php ./get-comments.php

Получить из всех PHP-файлов:

find -name '*.php' -print0 | xargs -0 cat | php ./get-comments.php >> all-comments.txt

Источник

PHP Comments

A comment in PHP code is a line that is not executed as a part of the program. Its only purpose is to be read by someone who is looking at the code.

  • Let others understand your code
  • Remind yourself of what you did — Most programmers have experienced coming back to their own work a year or two later and having to re-figure out what they did. Comments can remind you of what you were thinking when you wrote the code

PHP supports several ways of commenting:

Example

Syntax for single-line comments:

// This is a single-line comment

# This is also a single-line comment
?>

Example

Syntax for multiple-line comments:

/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
?>

Example

Using comments to leave out parts of the code:

// You can also use comments to leave out parts of a code line
$x = 5 /* + 15 */ + 5;
echo $x;
?>

Unlock Full Access 50% off

COLOR PICKER

colorpicker

Join our Bootcamp!

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

Thank You For Helping Us!

Your message has been sent to W3Schools.

Top Tutorials
Top References
Top Examples
Get Certified

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Источник

Оцените статью