请告诉我一个简单有效的方法来阅读或获取信息(无论它是什么?)形成php文件中的注释,而我知道wordpress使用此功能。
就像我有一个名为theme
的文件夹,这个评论有三个文件:
在第一个档案中
/* Template Name: page */
在第二个档案中
/* Template Name: category */
在第三档
/* Template Name: Tags */
然后我在数组中获得page
,category
,tags
的方式,
请给出解决方案
感谢您的时间
答案 0 :(得分:2)
要削减php文件,你可以这样做:
$str = file_get_contents("/path/to/file1");
比你完成php文件代码那样,你需要使用一些正则表达式来获取评论内容
让我们假设$str
有你的php源代码,正则表达式可能是这样的:
$str = "/* Template Name: page */\n bla bla blah";
preg_match( "#/\* Template Name: (\w*) \*/#", $str, $m);
var_dump( $m );
以前的代码输出:
array(2) {
[0]=>
string(25) "/* Template Name: page */"
[1]=>
string(4) "page"
}
您需要的内容存储在$m[1]
答案 1 :(得分:1)
$source = file_get_contents($filename);
$tokens = token_get_all($source);
foreach($tokens as $token){
if( ($token[0] == T_COMMENT || $token[0] == T_DOC_COMMENT)
&& preg_match('/Template Name: (.*)/',$token[1],$match){
echo "Template name is ".$match[1];
}
}