【问题标题】:PHP Eval that evaluates HTML & PHP评估 HTML 和 PHP 的 PHP Eval
【发布时间】:2010-11-21 12:59:48
【问题描述】:

我在搞乱模板,我遇到了一种情况,我需要向浏览器回显一个包含 html 和 php 的模板。如何评估 PHP 并将其发送到浏览器?

所以这里有一个例子(main.php):

    <div id = "container">
        <div id="head">
            <?php if ($id > 10): ?>
                <H3>Greater than 10!</H3>
            <?php else: ?>
                <H3>Less than 10!</H3>
            <?php endif ?>
        </div>
            </div>

然后在template.php中:

   <?php
           $contents; // Contains main.php in string format
           echo eval($contents); // Doesn't work... How do I do this line??
   ?>

编辑:我的模板还允许您从控制器 Smarty 样式注入数据。输出缓冲区会允许我这样做,然后评估我的 php.ini 吗?理想的情况是它首先通过代码并首先评估所有标签,然后运行 ​​php.ini 文件。这样我就可以使用从我的控制器发送的数据创建循环和东西。

So maybe a more complete example: 
    <div id = "container">
            <div id = "title">{$title}</div> <!-- This adds data sent from a controller -->
            <div id="head">
                <?php if ($id > 10): ?>
                    <H3>Greater than 10!</H3>
                <?php else: ?>
                    <H3>Less than 10!</H3>
                <?php endif ?>
            </div>
    </div>

谢谢!

【问题讨论】:

    标签: php templates


    【解决方案1】:

    如果您尝试使用混合的 HTML/PHP 字符串执行此操作(例如来自数据库,就像我一样),您可以这样做:

    eval(' ?>'.$htmlandphp.'<?php ');
    

    更多信息:http://blog.5ubliminal.com/posts/eval-for-inline-php-inside-html/(注意这是截至 2014-3-3 的死链接)

    【解决方案2】:

    改用输出缓冲。 eval() 是出了名的慢。

    main.php:

    <div id="container">
        <div id="title"><?php echo $title; ?></div><!-- you must use PHP tags so the buffer knows to parse it as such -->
        <div id="head">
            <?php if ($id > 10): ?>
                <H3>Greater than 10!</H3>
            <?php else: ?>
                <H3>Less than 10!</H3>
            <?php endif ?>
        </div>
    </div>
    

    您的文件:

    $title = 'Lorem Ipsum';
    $id = 11;
    
    ob_start();
    require_once('main.php');
    $contents = ob_get_contents();
    ob_end_clean();
    
    echo $contents;
    

    这个输出将是:

    Lorem Ipsum

    大于 10!

    【讨论】:

    • 是的,您可以使用已在模板中声明的变量。但是,您不能像以前那样使用 {$title} - 这只会按字面意思显示该文本。你必须告诉模板那是 PHP。我已经更新了我的答案来演示。
    • ob_start()...哇!每天都有新东西!我喜欢 StackOverlow!我喜欢 php!如此强大的语言...
    【解决方案3】:

    不要读取文件,而是包含它并使用输出缓冲区来捕获结果。

    ob_start();
    include 'main.php';
    $content = ob_get_clean();
    
    // process/modify/echo $content ...
    

    编辑

    使用函数生成新的变量作用域。

    function render($script, array $vars = array())
    {
        extract($vars);
    
        ob_start();
        include $script;
        return ob_get_clean();
    }
    
    $test = 'one';
    echo render('foo.php', array('test' => 'two'));
    echo $test; // is still 'one' ... render() has its own scope
    

    【讨论】:

    • 嗯.. 那我该如何注入数据呢?
    • 任意数量的方式。在 $content 上进行字符串替换,在包含 main.php 之前使全局变量可用于 main.php,这个列表还在继续。
    • 我认为这样行不通。在您有机会进行字符串替换之前,包含会抛出错误。
    【解决方案4】:

    在您的情况下,最好的解决方案是结合 evaloutput buffer

    // read template into $contents
    // replace {title} etc. in $contents
    $contents = str_replace("{title}", $title, $contents);
    ob_start();
        eval(" ?>".$contents."<?php ");
    $html .= ob_get_clean();
    echo html;
    

    【讨论】:

      【解决方案5】:
      $contents = htmlentities($contents);
      echo html_entity_decode(eval($contents));
      

      【讨论】:

      • 为了提高您的回答质量,请说明您的帖子将如何/为何解决问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-20
      • 2017-01-21
      • 1970-01-01
      • 1970-01-01
      • 2011-08-21
      相关资源
      最近更新 更多