我有这个HTML / PHP内容(作为字符串):
<html>
<?php my_class->my_function('test.php', 'value1', 'value2');
<?php my_class->my_function('test2.php', 3);
</html>
my_class-&gt; my_function的作用包括内容,具体取决于发送的值。
看起来像这样
function my_function($file, $value1, $value2)
{
include $file . $value1 . $value2;
}
应输出my_function的结果,而不是函数调用。
结果应为
<html>
<?php
/* Content from file test.php */
echo 'This file is test.php, with value1 and value2';
?>
<?php
/* Content from file test2.php */
echo 'This file is test2.php, with value 3';
?>
</html>
我想我需要某种preg_replace与eval,include或get_file_contents结合使用。
答案 0 :(得分:0)
答案不完整。这是我在使用爆炸时首先得到的。它工作但是在向函数添加多个值时会产生错误。它可以处理的一个或两个值。不多,而且对我来说太复杂了。
function include_layout2($part, $file = '/index.php')
{
$url = get_layout2($part, $file);
if(file_exists($url))
{
return get_include_contents( $url );
}
}
function get_layout2($part, $file = '/index.php')
{
$layout_theme = 'default';
$addmod_layout = get_option('addmod_layout');
$layout = $addmod_layout['styles'][$layout_theme][$part]['layout'];
$layout = (!empty($layout)) ? $layout : 'default';
$url = TEMPLATEPATH . '/styles/' . $part . '/' . $layout . $file;
if(file_exists($url))
{
return $url;
}
}
function generate_content($layout = 'index')
{
$content = include_layout2($layout);
$content_a = explode("<?php addmod_include('", $content);
$save_for_later = $content;
$counter = 0;
while(count($content_a) > 1)
{
$content_a = explode("<?php addmod_include('", $save_for_later);
$output = '';
$save_for_later = '';
for($i = 0; $i < sizeof($content_a); $i++)
{
$content_b = explode("'); ?>", $content_a[$i], 2);
if(isset($content_b[0]) && !empty($content_b[0]))
{
$include_test = include_layout2($content_b[0]);
if(isset($include_test))
{
$content_b[0] = include_layout2($content_b[0]);
}
else
{
$new_layout = explode("addmod_include('", $content_b[0]);
$new_file = explode("addmod_include(', '", $content_b[0]);
if(isset($new_layout[0]) && isset($new_file[1]))
{
$content_b[0] = include_layout2($new_layout[0], $new_file[1]);
}
else
{
$content_b[0] .= "'); ?>";
}
}
}
$content = implode($content_b);
$output .= $content;
$save_for_later .= $content;
}
$counter++;
}
return $output;
}
echo generate_content();
不要试图运行代码,因为它有点依赖于其他一些东西。
我不知道这是否能让我更清楚我需要做什么?