[英]Get code between php tags with regular expression
我正在尝试从HTML模板文件中获取php代码以执行它并将结果放回去。 我可以使用哪些正则表达式代码? 有没有一种方法也可以返回找到的第一个标签的位置?
<p>some html</p>
<?php $some = "php code"; ?>
<p>some <em>more</em> html</p>
<?php $some = "more php code"; ?>
我想从html过滤掉php代码。
而不是“拿出PHP放回PHP”,您应该将变量传递给模板文件。
像这样:
<?php
function loadTemplate($path,$data=array()){
if (file_exists($path) === false){
throw new Exception('Template not found:'.$path);
return false;
}
extract($data);
ob_start();
require($path);
$return = ob_get_contents();
ob_end_clean();
return $return;
}
$vars = array('var1'=>$value,
'var2'=>$somevalue,
'var3'=>$someothervalue,
'var4'=>$blab);
$template = loadTemplate('path/to/thefile.php',$vars);
?>
然后从文件file.php中访问$vars
数组值,例如echo $var1
echo $var2
ect
希望能帮助到你
preg_match("/<\?.*?\?>/m",$output,$matches)
要么
preg_match("/<\?.*?\?>/s",$output,$matches)
应该匹配所有行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.