[英]PHP read file comments NOT file content - forgotten
Sorry folks forgotten this one, I need to read the first "batch" of comment in a php file example would be: 对不起人们忘记了这一个,我需要在php文件中读取第一批“评论”的例子:
<?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 我需要阅读另一个文件中的第一条评论,但我完全忘记了如何获取/ **这是一些基本的文件信息** /作为一个字符串抱歉,但感谢adavance
There's a token_get_all($code)
function which can be used for this and it's more reliable than you first might think. 有一个
token_get_all($code)
函数可用于此,它比你想象的更可靠。
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) )
continue;
// Do something with the comment
$txt = $token[1];
}
?>
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];
}
Is this what you mean? 你是这个意思吗?
$file_contents = '/**
sd
asdsa
das
sa
das
sa
a
ad**/';
preg_match('#/\*\*(.*)\*\*/#s', $file_contents, $matches);
var_dump($matches);
Use this: 用这个:
preg_match("/\/\*\*(.*?)\*\*\//", $file, $match);
$info = $match[1];
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);
然后只需调用:
$comments = find_between('/\\*\\*','\\*\\*/',$myfileline);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.