读取包含 BOM 的 UTF-8 编码的文本文件时,可以使用 PHP 的 file_get_contents() 函数读取文件内容,然后使用 preg_replace() 函数将 BOM 删除。

admin2024-09-30总结分享4
<?
//读取包含 BOM 的 UTF-8 编码的文本文件时,可以使用 PHP 的 file_get_contents() 函数读取文件内容,然后使用 preg_replace() 函数将 BOM 删除。
$file = 'playlist.txt';
$content = file_get_contents($file);
$content = preg_replace('/\x{EF}\x{BB}\x{BF}/', '', $content);

//这段代码的作用是将处理后的文本内容存储到 $content 变量中的内容写回到原文件 example.txt 中。
//这样就可以去掉 UTF-8 编码下的 BOM,读取和处理文本内容。
file_put_contents($file, $content);
?>