加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

如何让php脚本为包含文件的每一行添加一个选项卡?

发布时间:2020-12-13 13:21:06 所属栏目:PHP教程 来源:网络整理
导读:在我的 HTML中,我有一个包含文件的 PHP脚本.此时,代码缩进了2个选项卡.我想做的是让php脚本为每一行添加两个选项卡.这是一个例子: 主页: body div ?php include("test.inc"); ? /div/body 并且“test.inc”: pThis is a test/pdiv pThis is a nested test
在我的 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在格式化文档时会继续删除它们.

在test.inc文件中,您可以使用 output buffering捕获PHP脚本的所有输出,然后再将其发送到浏览器.然后,您可以对其进行后处理以添加所需的选项卡,然后将其发送.在文件的顶部,添加
<?php
  ob_start();
?>

最后,添加

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

我不一定订阅这个解决方案 – 它可能是内存密集型的,具体取决于您的输出,并会阻止您的包含文件在工作时向客户端发送部分结果.您可能最好重新格式化输出,或使用某种形式的自定义“打印”包装器来标记事物(并使用heredocs的打印作为常量HTML输出).

编辑:使用str_replace,如评论所示

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读