tags:

views:

103

answers:

5

Sorry folks forgotten this one, I need to read the first "batch" of comment in a php file example would be:

<?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 have totally forgotten how to get the /** This is some basic file info **/ as a string Sorry but thanks in adavance

A: 

Is this what you mean?

$file_contents = '/**

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

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

var_dump($matches);
Kieran Allen
kieran - looks like you're having 'one of those' type mondays as well. nice to 'bump' into you again :)
jim
heh, yeah i'm having one of those really productive days at work.. *cough*
Kieran Allen
+1  A: 

there's a SO question here that does similar stuff:

http://stackoverflow.com/questions/2749301/how-to-parse-phpdoc-style-comment-block-with-php

jim
A: 

Use this:

preg_match("/\/\*\*(.*?)\*\*\//", $file, $match);
$info = $match[1];
galambalazs
+5  A: 

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):

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

?>
svens
A: 

function find_between($from,$to,$string){

$cstring = strstr($string, $from);

$newstring = substr(substr($cstring,0,strpos($cstring,$to)),1);

return $newstring;

}

then just call : $comments = find_between('/**','**/',$myfileline);

Wright-Geek