如何从文件中读取变量(其中一些包含PHP代码),并解析PHP代码


How can I read variables (some of them containing PHP code) from a file, and parse the PHP code?

我必须有PHP文件,其中一个包含变量,其中一些包含PHP代码:

/* variables.php */
<?php
    $oneVariable = "Hello";
    $anotherVariable = "<<<EOF
<h1>Headline</h1>
<p>Text</p>
<?php echo "I whish this would work!" ?>
EOF;

我访问的另一个:

/* output.php */
include("variables.php");
echo "$oneVariable World!";
echo "$anotherVariable";

当通过浏览器访问output.php时,我会得到:

Hello World!
Headline
Text
<?php echo "I whish this would work!" ?>

但我希望对heredoc中的php代码进行解析,这样我只能看到"我希望这能工作!"。

我如何才能做到这一点

编辑:不想通过编辑我原来的例子来破坏问题,所以这里有另一个例子:

我想能够有一个模板文件:

/* index.php */
<?php
include("article.php");
?>
<title><?php echo "$title" ?></title>
<?php echo "$articleContents" ?>

还有一堆包含文章数据的文件,例如这个:

/* article.php */
<?php
$title = "About grapes";
$articleContents = <<<EOF
<h1>The wrath</h1>
<p>Even though the title of this article is <?php echo "$title" ?>,
     what I'm personally really into is raisins!</p>
EOF;

当我请求index.php时,我想获得"格式化"的文章。

这只是一个语法错误,不能在字符串声明中嵌入php标记。假设您确实需要在创建$anotherVariable字符串时使用某种逻辑,那么您只需要在声明之前构造这些部分:

$oneVariable = "Hello";
$message = "I wish this would work!";
$anotherVariable = "<<<EOF
    <h1>Headline</h1>
    <p>Text</p>
    $message
EOF;

如果$anotherVariable要包含更复杂的模板,另一种选择是使用输出缓冲:

$oneVariable = "Hello";
//start output buffering
ob_start(); 
//now exit php mode, and define the template, inlcluding html and php interspersed
?>
<h1>Headline</h1>
<p>Text</p>
<?php echo "I whish this would work!" ?>
<p>Text</p>
<ul>
<?php foreach([1,2,3] as $el):?>
    <li>item number <?php echo $el;?> </li>
<?php endforeach;?>
</ul>
<?php
//save output to variable
$anotherVariable = ob_get_clean();

试试这个:

function render($path, $data = ''){
    foreach ($data as $key => &$value) {
        //parse the contents of each variable
        ob_start();
        eval('?>' . $value);
        $value = ob_get_clean();
    }
    //bundle variables with template
    ob_start();
    extract($data);
    require $path;
    $content = ob_get_clean();
    return $content;
}
$title = "About grapes";
$articleContents = <<<EOF
<h1>The wrath</h1>
<p>Even though the title of this article is <?php echo "$title" ?>,
     what I'm personally really into is raisins!</p>
EOF;
$data = compact('title', 'articleContents');
echo render('t.txt', $data);

模板(t.txt):

<h1><?php echo $title; ?></h1>
<hr>
<article>
    <p><?php echo $articleContents; ?></p>
</article>
<?php echo "I whish this would work!" ?>

替换为:

I whish this would work!

输出缓冲器可能比HEREDOC更可取。

<?php
$var_one = 'I wish you were here.';
ob_start();
?>
     <h1>Running over the same old ground</h1>
     <p>Something <?php echo some_func('blah'); ?> wicked this way comes.</p>
<?php
$var_two = ob_get_clean();

如果需要,你可以将html放在另一个文件中:

ob_start();
include '/path/to/file.php';
$output = ob_get_clean();

实现您想要的目标没有简单明了的方法。最简单的方法是将实际模板保存在一个单独的文件中,并将其includeit:

/* variables.php */
<?php
$oneVariable = 'Hello';
$template = 'foo.php';
/* output.php */
include 'variables.php';
echo "$oneVariable World!";
include $template;