【问题标题】:str_replace include return 1str_replace 包括返回 1
【发布时间】:2014-02-17 03:10:25
【问题描述】:
$markup_array = array();
    $name_array = array();
    while($array = $query->fetch_assoc()) {
    $name_array = $array['plugin_name'];
    $markup = '{$'.$array['plugin_name'].'}';
    }
$callback = str_replace($markup, include("plugins/".$name_array."/".$name_array.".php"), $buffer);

这是完整的代码

function callback($buffer)
{
GLOBAL $mysqli;
GLOBAL $db_prefix;
GLOBAL $parameters;

$query = $mysqli->query("...query...");
$count = $query->num_rows;
    if($count > 0) {
    $markup_array = array();
    $name_array = array();
    while($array = $query->fetch_assoc()) {
    $name_array = $array['plugin_name'];
    $markup = '{$'.$array['plugin_name'].'}';
    }
$callback = str_replace($markup, include('plugins/'.$name_array.'/'.$name_array.'.php'), $buffer);
  }

  return $callback;
}

function load_theme() {
GLOBAL $mysqli;
GLOBAL $db_prefix;
GLOBAL $parameters;
$query = $mysqli->query("...query...");
$count = $query->num_rows;
    if($count > 0) {
    $theme_parameters = array();
    $array = $query->fetch_assoc();
    $theme_parameters['ID_THEME'] = $array['ID_THEME'];
    $theme_parameters['theme_name'] = $array['theme_name'];
    $theme_parameters['theme_version'] = $array['theme_version'];
    $theme_parameters['theme_folder'] = 'themes/'.$array['theme_folder'].'/';
    $theme_parameters['theme_status'] = $array['theme_status'];

//loading pages
ob_start("callback");
require_once($theme_parameters['theme_folder'].'/header.php');
require_once($theme_parameters['theme_folder'].'/body.php');
require_once($theme_parameters['theme_folder'].'/footer.php');
ob_end_flush();

我要包含的文件:

<ul id="horizontalmenu">
<li class="parent">Home</li>
<? 
$query_pages = $mysqli->query("...query...");
while($pages_array = $query_pages->fetch_assoc()) 
    { 
echo '<li class="parent"><a href="'.$pages_array['ID_PAGE'].'_'.text2url($pages_array['title']).'.html">'.$pages_array['title'].'</a>';
list_pages_home_child($id_parent);
echo '</li>';
    }
    ?>
<li class="parent">Contact</li>
</ul>

因此,包含返回 1。关于如何返回包含的文件内容而不仅仅是“1”的任何线索? 在我要包含的 php 文件中,有 $var 和函数,所以 file_get_contents() 是不够的。我得到Fatal error: Unknown: Cannot use output buffering in output buffering display handlers in Unknown on line 0

【问题讨论】:

  • 如何不只得到 1...根据常识,没有人想要包含“1”
  • 阅读documentation for include()Successful includes, unless overridden by the included file, return 1.
  • 我已经检查过了,问题是我不知道如何覆盖...?
  • @Dorian_gd:问题是你在这里使用了错误的函数。 include() 包括 AND 评估指定的文件。你只需要在这里file_get_contents()
  • 在 $name_array.php 我有 php,file_get_contents() 只返回 html

标签: php arrays include str-replace


【解决方案1】:

去掉文档中的这个include example,您需要在$name_array . "/" . $name_array . ".php" 文件中包含return 语句才能从包含文件中获取任何内容。

return.php

<?php
    $var = 'PHP';
    return $var;
?>

noreturn.php

<?php
    $var = 'PHP';
?>

testreturns.php

<?php
    $foo = include 'return.php';
    echo $foo; // prints 'PHP'

    $bar = include 'noreturn.php';
    echo $bar; // prints 1
?>

如果您想要文件中的实际 PHP 代码,您应该使用 file_get_contents()

【讨论】:

  • 好吧,它解决了一半的问题。这种方式看起来不能使用函数
  • 感谢您的帮助。这样,I get Fatal error: Unknown: Cannot use output buffering in output buffering display handlers in Unknown on line 0
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多