【问题标题】:Weird behaviour when trying to replace two php function results in html尝试替换两个 php 函数时的奇怪行为导致 html
【发布时间】:2012-06-03 22:57:13
【问题描述】:

模板文件是 .php 并且有这些占位符:

<ul>
<li><a href='a.php'>{{placeholder1}}</a></li>
{{placeholder2}}
</ul>

这是替换它们的代码:

$file = file_get_contents($template);
$file = str_ireplace('{{placeholder1}}', count_messages(), $file);
$file = str_ireplace('{{placeholder2}}', show_link(), $file);
print $file;

函数没什么特别的('functions.php'):

function count_messages()
{
  ob_start();
  if (preg_match('/^[A-Za-z0-9_]{3,40}$/', $_SESSION['username']))
  {
    $table = $_SESSION['username'];
  }
  else
  {
    header('Location: login.php');
    exit();
  }

  try
  {
    $db = new PDO('sqlite:site.db');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $result = $db->prepare("SELECT Count(*) FROM `$table` WHERE `isRead` = '0'");
    $result->execute();
    $count = $result->fetchColumn();
    if ($count > 0)
    {
      print "<b style='color: #00ff00; text-decoration: blink;'>$count</b> <b style='font-size: 6pt; text-decoration: blink; text-transform: uppercase;'>unread</b> ";
    }
    unset($db);
  }

  catch(PDOException $e)
  {
    echo $e->getMessage();
  }
  ob_end_flush();
}

function show_link()
{
  ob_start();
  if ($_SESSION['username'] == "admin")
  {
    print "<li><a href='admin_panel.php' target='main_iframe'><b style='color: #ffff00;'>Admin Panel</b></a></li>;
  }
  ob_end_flush();
}

首先使用一些样式计算消息并输出编号,如果用户名是“admin”,则第二个添加到菜单“管理面板”链接。

问题是(php日志中没有错误): count_messages() 有效,但在页面上的所有元素上方输出“n unread”。 show_link() 不输出链接。

文件$template是可读的并且命名为template.php:

<?php

session_start();

if(!$_SESSION['islogged'])
{
  header('Location: login.php');
    exit();
}

require_once('functions.php');

?>
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta charset="UTF-8" />
<meta name="description" content="Documents" />           
<link rel="stylesheet" type="text/css" href="style.css" />
<title>Documents</title>
</head>
<body>

<div id="main">
<iframe src="documents.php" name="main_iframe" id="main_iframe">
</iframe>
</div>

<div id="main_menu">
<ul id="menu_list">
<li><a href="messages.php" target="main_iframe">{{placeholder1}}Messages</a></li>
{{placeholder2}}
<li><a href="logout.php" style="font-weight: bold; color: #ff0000">Log out</a></li>
</ul>
</div>
</body>
</html>

index.php:

<?php

session_start();

require_once('functions.php');

$template = 'template.php';

if (file_exists($template))
{
  if (is_readable($template))
  {
    if(!$_SESSION['islogged'])
    {
      session_destroy();
      header('Location: login.php');
      exit();
    }
  }
  else
  {
    print "Template file cannot be opened";
  }
}
else
{
  print "Template file doesn't exist";
}

$file = file_get_contents($template);
$file = str_ireplace('{{placeholder1}}', count_messages(), $file);
$file = str_ireplace('{{placeholder2}}', show_link(), $file);
print $file;
?>

我希望这里有人知道导致这种行为的原因......

【问题讨论】:

  • 绝对不可能有一个名为count() 的用户定义函数——这样做会在解析时立即出现致命错误。请出示您的实际代码。
  • 我只是为这个问题命名了它们。我会填写他们的真实姓名。编辑:完成
  • 您是否尝试过简单地删除ob_start()/ob_end_flush() 调用?它们似乎没有做任何有用的事情,可能会导致输出排序问题。
  • 哪个调用remove,就这两个函数中的这些?
  • 另外你应该(?...我的意见,YMMV)echo instead of print

标签: php function templates replace


【解决方案1】:

您在 str_ireplace 函数调用中使用函数的结果值,但函数没有返回任何内容,它们缺少 return 语句。

您可能打算在代码中使用return ob_get_clean(); 而不是ob_end_flush();

【讨论】:

  • 好吧,我返回了结果,但我在两个占位符中都只有数字 1:1Messages for first 1 for second
  • ob_end_flush() 将结果写入输出。你在考虑ob_get_clean()吗?
  • @KonradRudolph 虽然您实际上一针见血。他基本上将NULL 传递给str_ireplace(),因为他可能应该调用ob_get_clean()...或者只是正确使用字符串变量
  • @KonradRudolph 现在为你 +1 :-D
  • 是的,我知道它现在不会返回任何内容,因为我已经对其进行了测试并将代码恢复为原始代码。现在使用 ob_get_clean() 甚至不会返回 'n unread' 问题。
猜你喜欢
  • 2015-09-06
  • 2016-06-21
  • 2012-03-20
  • 2012-04-25
  • 2014-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-27
相关资源
最近更新 更多