我有三个php文件:engine.php,links.php和test.php
理论上,当我在test.php中调用_insert()
函数时,它应该用links.php的输出替换一个字符串,而忽略ob_start()
和ob_get_clean()
,并输出links.php刚刚回应。
engine.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);
}
}
links.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());
?>
test.php的
<?php
require("engine.php");
db();
echo _include("<div><!--Include:links.php--></div>");
?>
它不会在div中输出一系列链接,而是从links.php中跳过返回完全结束的回声,就好像只有一个包含而没有ob_start()
和ob_get_clean()
。
为什么?
答案 0 :(得分: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);
}
}