总结:我正在尝试替换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()
??
答案 0 :(得分:0)
如果你只是将你的标签数据放入一个数组,然后它可以很容易地迭代,那就简单多了:
$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 );
}