在 php 中使用 utf-8 并加载文件 (require_once) 会产生奇怪的字符

Using utf-8 in php and loading file (require_once) gives strange char

在我使用 require_once('something.php'); 的地方,我在 html 中有奇怪的字符,当我在验证器上检查页面时:

Validation Output: 1 Error 
Error Line 1, Column 1: character "" not allowed in prolog 

这仅在我使用 UTF-8 时发生。早些时候我有 ANSI 格式的文件,没问题。

是的,我正在更改元数据并将所有文件保存为 UTF-8 我可以将代码移动到节主体,但这对我来说很奇怪。

这很可能是由 UTF-8 BOM (Byte Order Mark) 引起的。在某些 HEX 查看器/编辑器中打开任何文件并检查该文件中的前 3 个字节。

Windows-1250 编码中的 UTF-8 BOM 如下所示:。或 ISO-8859-1 中的 。这是十六进制的 EF BB BF

只需将您的文件保存为 UTF-8 无 BOM。例如 Notepad++ editorFormat 菜单下有两个选项:

  • 转换为 UTF-8(无 BOM)
  • 转换为 UTF-8