如何在PHP中使用HTML作为模板?
我想使用html作为模板,并用PHP填充数据。如果我有一些这样的html如何在PHP中使用HTML作为模板?,php,html,parsing,templates,Php,Html,Parsing,Templates,我想使用html作为模板,并用PHP填充数据。如果我有一些这样的html <h1>${heading-1}</h1> <p>${paragraph}</p> 它改变了html,就像 <h1>You can't do anything</h1> <p>Unless you show your code which you don't have so be ready for down votes</p>
<h1>${heading-1}</h1>
<p>${paragraph}</p>
它改变了html,就像
<h1>You can't do anything</h1>
<p>Unless you show your code which you don't have so be ready for down votes</p>
你什么都做不了
除非你展示了你没有的代码,否则就准备好投票吧
我怎样才能做到这一点?我应该使用正则表达式吗?您可以像这样使用foreach循环和stru替换
$html = '<h1>${heading-1}</h1>
<p>${paragraph}</p>';
$possible_matches = array(
'heading-1'=> 'You can\'t do anything',
'paragraph' => 'Unless you show your code which you don\'t have so be ready for down votes'
);
foreach($possible_matches as $key=> $value){
$html = str_replace('${'.$key.'}',$value,$html);
}
echo $html;
$html='${heading-1}
${paration}';
$mablue_matches=数组(
'heading-1'=>'你什么都做不了',
'段落'=>'除非您显示您没有的代码,否则请准备好进行否决投票'
);
foreach($key=>$value可能匹配){
$html=str_replace('${'.$key.'}',$value,$html);
}
echo$html;
如果html模板包含在外部文件中,您可以使用file_get_contents将html放在变量$html中请检查此创建模板您是如何阅读问题的,还是只是通过标题来假设它的?事实上,@Tegito123的建议是正确的。不使用正则表达式()解析HTML
$html = '<h1>${heading-1}</h1>
<p>${paragraph}</p>';
$possible_matches = array(
'heading-1'=> 'You can\'t do anything',
'paragraph' => 'Unless you show your code which you don\'t have so be ready for down votes'
);
foreach($possible_matches as $key=> $value){
$html = str_replace('${'.$key.'}',$value,$html);
}
echo $html;