我在包含多个 html 页面时遇到了问题。 我有主要的 index.html,但它开始有点太复杂了,所以我决定将它分成多个 html 文件,每个文件都使用 php 导入到单个 index.php 中。
index.php文件
<?php
include('head.html');
include('header.html');
include('slideshow.html');
include('pictureGalery.html');
include('footer.html');
include('closer.html');
?>
使用 Google Chrome Developer Tool 我发现,php 还包括一些空格(您可以在 div 标题、内容、容器等之间的图片中看到它们... 通过一些谷歌搜索,我发现了一些关于这个问题的文章,比如:
- PHP include causes white space at the top of the page (我不能使用 edit cmd 命令,因为我有 win7 64 位并且它没有在那里实现。你只能使用“打开记事本”命令,但这些空格在记事本中不可见。
- UTF-8 Without BOM?
- PHP include() before doctype, causing a white space
我还尝试导入 reset.css 文件 ( http://meyerweb.com/eric/tools/css/reset/ ) 但它也没有用。 唯一 似乎对我有效 的方法是发布 cebasso ( https://stackoverflow.com/a/14362246/1784053 )。但我觉得这种方式有点过于激进和低效。
关于如何解决这个问题还有其他想法吗?
最佳答案
然后,最终这是由包含的文件中的意外 BOM 引起的: 为方便日后引用,我只是简单地贴出PHP检查的代码。
print_r(array_map('dechex', array_map('ord', str_split(file_get_contents("xxxxxxxxxxxxx.html")))));
我肯定更喜欢使用Notepad++ 或其他软件来保存非BOM 版本, 但如果仍然喜欢使用 PHP 删除它们,只需使用: 这会在您每次执行时永久删除文件中的前 3 个字符,因此只运行一次。
$filename="xxxxxxx.html";
$filec = file_get_contents($filename);
file_put_contents($filename,substr($filec, 3, strlen($filec)));
希望对你有帮助:D 祝你好运!
关于php include html 添加空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18773912/