0

我有三个 php 文件:engine.php、links.php 和 test.php

理论上,当我_insert()在 test.php 中调用函数时,它应该用 links.php 的输出替换一个字符串,而不是ob_start()ob_get_clean()忽略,而 links.php 的输出只是被回显。

引擎.php

function db() {
    require("config.php");
    $conn = @mysql_connect($host, $uname, $pass) or die("DB error: ".mysql_error());
    @mysql_select_db($db) or die("DB error: ".mysql_error());
    @mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'"); #UTF-8 FIX
}

function _include($x) {
    if (preg_match("/<!--Include:(.*)-->/", $x, $matches)){

        ob_start();
        include($matches[1]);
        $output = ob_get_clean();

        return preg_replace("/<!--Include:(.*)-->/", $output, $x);
    }

}

链接.php

<?php
$query = @mysql_query("SELECT title, description, url FROM links ORDER BY id") or die("DB error: ".mysql_error());

while($row = @mysql_fetch_array($query)) {
    $url = $row["url"];
    $title = $row["title"]; 
    $description = $row["description"];
    echo "<a href=\"$url\">$title</a>: $description";
}

@mysql_close($conn) or die(mysql_error());
?>

测试.php

<?php

require("engine.php");

db();

echo _include("<div><!--Include:links.php--></div>");

?>

它没有在 div 中输出一系列链接,而是跳过了从 links.php 返回的完全结束回显,就好像只有一个 include 和 no ob_start()and一样ob_get_clean()

为什么?

4

1 回答 1

0

您可以从 links.php 返回一个字符串:

<?php
$query = @mysql_query("SELECT title, description, url FROM links ORDER BY id") or die("DB error: ".mysql_error());

$links =''; //

while($row = @mysql_fetch_array($query)) {
    $url = $row["url"];
    $title = $row["title"]; 
    $description = $row["description"];
    $links .= "<a href=\"$url\">$title</a>: $description";//
}

@mysql_close($conn) or die(mysql_error());
return $links;//

然后,在 engine.php 中:

function _include($x) {
    if (preg_match("/<!--Include:(.*)-->/", $x, $matches)){

        //ob_start();
        $output = include($matches[1]);
        //$output = ob_get_clean();

        return preg_replace("/<!--Include:(.*)-->/", $output, $x);
    }

}
于 2013-09-12T09:26:48.840 回答