【问题标题】:PHP replace {replace_me} with <?php include ?> in output bufferPHP 在输出缓冲区中用 <?php include ?> 替换 {replace_me}
【发布时间】:2015-01-23 06:55:19
【问题描述】:

我有一个这样的文件

**buffer.php**

ob_start();
<h1>Welcome</h1>

{replace_me_with_working_php_include}

<h2>I got a problem..</h2>
ob_end_flush();

缓冲区中的所有内容都是使用数据库中的数据动态生成的。 并且将php插入数据库不是一种选择。

问题是,我得到了我的输出缓冲区,我想用一个有效的 php 包含替换“{replace}”,它包含一个也有一些 html/php 的文件。

所以我的实际问题是:如何在输出缓冲区中用工作的 php 代码替换字符串?

希望你能帮上忙,在这方面已经用了很多时间了。

最好的问候 - user2453885

编辑 - 25/11/14

我知道 wordpress 或 joomla 正在使用一些类似的功能,你可以在你的帖子中写 {rate},它会用评级系统(一些 rate-plugin)代替它。这就是我想要的秘密知识。

【问题讨论】:

  • 也许你可以从他们身上学到一些东西(WordPress):The Shortcode API
  • 感谢您为我指明了正确的方向,不幸的是,这不是它背后的技术。但在有人发布答案之前,我会尝试在 wordpress 中找到我需要的东西。 - 再次感谢

标签: php include buffer output str-replace


【解决方案1】:

您可以使用 preg_replace_callback 并让回调包含您要包含的文件并返回输出。或者您可以用文本包含替换占位符,将其保存为文件并包含该文件(某种编译的东西)

【讨论】:

  • 是的,我确信这是有道理的,我现在正在 wordpress 代码中阅读类似的内容。我稍后再回来查看。感谢您的输入。 - user2453885
【解决方案2】:

对于简单的文本,您可以进行爆炸(尽管对于大块文本可能不是最有效的):

function StringSwap($text ="", $rootdir ="", $begin = "{", $end = "}") {
            // Explode beginning
            $go =   explode($begin,$text);
            // Loop through the array
            if(is_array($go)) {
                    foreach($go as $value) {
                            // Split ends if available
                            $value  =   explode($end,$value);
                            // If there is an end, key 0 should be the replacement
                            if(count($value) > 1) {
                                    // Check if the file exists based on your root
                                    if(is_file($rootdir . $value[0])) {
                                            // If it is a real file, mark it and remove it
                                            $new[]['file']  =   $rootdir . $value[0];
                                            unset($value[0]);
                                        }
                                    // All others set as text
                                    $new[]['txt']   =   implode($value);
                                }
                            else
                                // If not an array, not a file, just assign as text
                                $new[]['txt']   =   $value;
                        }
                }
            // Loop through new array and handle each block as text or include
            foreach($new as $block) {
                    if(isset($block['txt'])) {
                            echo (is_array($block['txt']))? implode(" ",$block['txt']): $block['txt']." ";
                        }
                    elseif(isset($block['file'])) {
                            include_once($block['file']);
                        }
                }
        }

    // To use, drop your text in here as a string
    // You need to set a root directory so it can map properly
    StringSwap($text);

【讨论】:

  • 感谢您的贡献,但它看起来不像我正在寻找的东西。 :)
  • 嗯,我以为您希望将您的 php 文件包含在这样的标签中:I want to run {/folder/file.php} in those tags 也许我读错了。
  • 你没看错,这不是我想要的选项。但是@Teyras 和 wordpress 做对了。我明天会回来回答我自己的问题。
【解决方案3】:

我可能在这里误解了一些东西,但是像这样简单的东西可能有用吗?

<?php

# Main page (retrieved from the database or wherever into a variable - output buffer example shown)
ob_start();

<h1>Welcome</h1>

{replace_me_with_working_php_include}

<h2>I got a problem..</h2>

$main = ob_get_clean();

# Replacement
ob_start();

include 'whatever.php';

$replacement = ob_get_clean();


echo str_replace('{replace_me_with_working_php_include}', $replacement, $main);

如果您也希望从该任务中删除输出缓冲区,也可以在包含文件中使用 return 语句。

祝你好运!

【讨论】:

    【解决方案4】:

    欢迎大家提供一些可爱的意见。

    我会尽量清楚地回答我自己的问题。

    问题:我首先认为我想实现一个 php 函数或包含在缓冲区中。然而,这不是我想要的,也不是有意的。

    解决方案:带有我想要的内容的回调函数。通过使用函数 preg_replace_callback(),我可以在缓冲区中找到我想要替换的文本,然后用回调函数返回的任何内容替换它。

    然后回调包含必要的文件/.classes 并使用其中包含书面内容的函数。

    如果你不明白,或者想详细说明/告诉我更多关于我的解决方案,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多