PHP reads file comments NOT file contents - forgot

Sorry that people forgot this, I need to read the first "batch" of comments in the php file example:

<?php
/** This is some basic file info **/
?>
<?php This is the "file" proper" ?>

I need to read the first comment inside another file, but I completely forgot how to get / ** This is the basic information about the file ** / as a string Sorry, but thanks in adavance

+3
source share
5 answers

There is a function here token_get_all($code)that can be used for this and is more reliable than you might think.

Here is a sample code to get all comments from a file (it has not been verified, but should be enough for you to get started):

<?php

    $source = file_get_contents( "file.php" );

    $tokens = token_get_all( $source );
    $comment = array(
        T_COMMENT,      // All comments since PHP5
        T_ML_COMMENT,   // Multiline comments PHP4 only
        T_DOC_COMMENT   // PHPDoc comments      
    );
    foreach( $tokens as $token ) {
        if( !in_array($token[0], $comment) )
            break;
        // Do something with the comment
        $txt = $token[1];
    }

?>
+13
source

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];
}
+1

, ?

$file_contents = '/**

sd
asdsa
das
sa
das
sa
a
ad**/';

preg_match('#/\*\*(.*)\*\*/#s', $file_contents, $matches);

var_dump($matches);
0

:

preg_match("/\/\*\*(.*?)\*\*\//", $file, $match);
$info = $match[1];
0
function find_between($from,$to,$string){
   $cstring = strstr($string, $from);
   $newstring = substr(substr($cstring,0,strpos($cstring,$to)),1);
   return $newstring;
}

: $comments = find_between('/\*\*','\*\*/',$myfileline);

0

Source: https://habr.com/ru/post/1754238/


All Articles