【问题标题】:File content not getting displayed correctly in div文件内容未在 div 中正确显示
【发布时间】:2017-04-28 10:12:22
【问题描述】:

我正在尝试将 php 文件的内容显示到 div。

<div contenteditable="true" id="highlight">
<?php
echo file_get_contents( "/path/test.php" );
?>
</div>

test.php

<?php
require_once (lib.php');

/*
some comments here
*/
session_cache_limiter('private, must-revalidate');
header('Pragma: public');
@ob_start();
@session_start();

error_reporting(E_ALL);
ini_set('display_errors', '1'); 

// INCLUDES
define('FPDF_FONTPATH', "path/font/");
// print_r($_GET);
?>
.......................

但 div 中的内容仅从这一行显示:// print_r($_GET);。我需要显示文件的全部内容。

谁能帮我解决这个问题?提前致谢。

【问题讨论】:

  • 你在浏览器中查看源代码了吗?
  • 开头的&lt;?php 标记被视为HTML 标记(在div 内)。
  • @JustOnUnderMillions..检查了源代码。那里显示了内容,但那些缺少的行以绿色显示。
  • @JustOnUnderMillions ..实际上我需要保存可编辑 div 上显示的文件内容。但是如果我使用highlight_file,它将返回内容以及高亮样式。有没有办法避免这种情况?

标签: php html file-get-contents


【解决方案1】:

因为浏览器的 HTML 解析器将 &lt;?php 视为 HTML 元素,所以它会被注释掉。您必须将 &lt;&gt; 转换为等效的 HTML 实体。

您可以使用htmlspecialchars 执行此操作(使用&lt;pre&gt;&lt;/pre&gt; 保留实际文件中的新行):

<div contenteditable="true" id="highlight">
    <pre><?php // this should be directly after pre to prevent white-space from appearing before the code from the file
        echo htmlspecialchars(file_get_contents( "43677696_test.php" ));
    ?>
    </pre>
</div>

你可以看到输出here

【讨论】:

  • 谢谢..这行得通。如果我使用highlight_file() 而不是file_get_contents(),在将 div 内容保存回文件时是否可以删除与其相关的代码?
  • 我不知道一个简单的解决方案,你必须解析highlight_file()的内容(第二个参数应该是true,见文档)然后你必须删除所有代码块(可能使用正则表达式)。
猜你喜欢
  • 2019-07-21
  • 1970-01-01
  • 1970-01-01
  • 2017-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多