PHP:有没有办法在另一个文件中循环使用PHP注释


PHP: Is there a way to loop through PHP annotations in another file?

摘要:我正在尝试替换一个html文件中的所有内容,这些内容包含多组给定的标记。对于每个集合,内容都将替换为特定的代码块。因为我有很多这样的标签"集合"和它们各自的代码,所以我把它们放在了tags.php文件中。然后cf_replace()在我的主test.php文件中调用它们;

详细信息:在我的test.php中,我有一个函数,它将用另一个文件中的内容替换两个给定的开始和结束标记之间的内容。

include('tags.php');
$testFile = ('someFile.htm');
    function cf_replace($start, $end, $new, $file) {
    // stuff
    return $file 
    };
cf_replace($start_htmlHead, $end_htmlHead, $cf_htmlHead, $testFile );
cf_replace($start_header, $end_header, $cf_header, $testFile );
// etc.

tags.php文件中,我声明了几个变量:

/**
* @Marker
*/
    $start_htmlHead= '<!-- Start Html_Head -->';
    $end_htmlHead= '<!-- End Html_Head -->';
    $cf_htmlHead= file_get_contents( './cf_templates/cf_htmlHead.txt' );
/**
* @Marker
*/
    $start_header= '<!-- Start Header -->';
    $end_header= '<!-- End Header -->';
    $cf_header= file_get_contents( './cf_templates/cf_header.txt' );
// etc.

我在理解注释标记时遇到了一些困难,所以我不知道如何在for循环中正确使用它们。

有没有一种方法可以循环浏览所有的@Markers,这样我就可以有一个更干净/重复更少的test.php文件——也就是说,不必一直写cf_replace()??

如果您只将标记数据放入一个数组中,那么它可以很容易地迭代:

$tags=[
    ['htmlHead']=>[
      '<!-- Start Html_Head -->',
      '<!-- End Html_Head -->'
    ],
    ['header']=>[
      '<!-- Start Header -->',
      '<!-- End Header -->'
    ]
];
//test.php
include('tags.php');
$testFile = ('someFile.htm');
function cf_replace($start, $end, $new, $file) {
    // stuff
    return $file 
};
foreach($tags as $key=>$tag){
    $txtfile = file_get_contents( './cf_templates/cf_' . $key . '.txt' );
    cf_replace($tag[0], $tag[1], $txtfile, $testFile );
}