8

在我的 HTML 中,我有一个包含文件的 php 脚本。此时,代码缩进 2 个制表符。我想做的是让 php 脚本在每一行添加两个选项卡。这是一个例子:

主页:

<body>
    <div>
        <?php include("test.inc"); ?>
    </div>
</body>

和“test.inc”:

<p>This is a test</p>
<div>
    <p>This is a nested test</p>
    <div>
        <p>This is an more nested test</p>
    </div>
</div>

我得到什么:

<body>
    <div>
<p>This is a test</p>
<div>
    <p>This is a nested test</p>
    <div>
        <p>This is an more nested test</p>
    </div>
</div>
    </div>
</body>

我想要的是:

<body>
    <div>
     <p>This is a test</p>
     <div>
            <p>This is a nested test</p>
            <div>
                <p>This is an more nested test</p>
            </div>
        </div>
    </div>
</body>

我意识到我可以将前导选项卡添加到包含文件中。但是,当我格式化文档时,VS 会不断删除它们。

4

3 回答 3

6

在您的 test.inc 文件中,您可以使用输出缓冲来捕获 PHP 脚本的所有输出,然后再将其发送到浏览器。然后,您可以对其进行后处理以添加所需的选项卡,然后继续发送。在文件顶部,添加

<?php
  ob_start();
?>

最后,添加

<?php
  $result = ob_get_contents();
  ob_end_clean();
  print str_replace("\t" . $result, "\n", "\n\t");
?>

我不一定订阅此解决方案 - 它可能会占用大量内存,具体取决于您的输出,并且会阻止您的包含文件在工作时将部分结果发送到客户端。您可能最好重新格式化输出,或者使用某种形式的自定义“打印”包装器来标记事物(并使用heredocs的打印来获得恒定的 HTML 输出)。

编辑:按照评论的建议使用 str_replace

于 2009-09-13T15:35:08.147 回答
1

我认为您的解决方案不容易完成。您可以考虑在将源代码呈现给客户之前使用HTML Tidy清理源代码。互联网上有很好的教程

于 2009-09-13T15:36:00.550 回答
0

最简单的解决方案是在包含文件中添加前导制表符,但不要使用文字制表符,而是使用\t转义序列。

于 2009-09-13T15:34:17.103 回答