PHP5中file
PHP5中的file_get_contents函数获取文件内容,实际是按二进制来读取的,所以,当你用file_get_contents去获取一个带BOM的UTF-8文件时,它并不会把UTF-8的BOM去掉,当你把读取的内容当作文本内容来进行一些操作时,可能会发生一些意想不到的结果。这并不能算
PHP5中的file_get_contents函数获取文件内容,实际是按二进制来读取的,所以,当你用file_get_contents去获取一个带BOM的UTF-8文件时,它并不会把UTF-8的BOM去掉,当你把读取的内容当作文本内容来进行一些操作时,可能会发生一些意想不到的结果。这并不能算作一个BUG,因为file_get_contents函数读取文件的时候,是按二进制来读取的,读取到的内容是包含BOM的,而用户操作的时候,想当然的以为读取到的内容是不包含BOM的文本内容(如用记事本打开后看到的内容),因为BOM在编辑软件中是不可见的,只有在十六进制模式下才可以看见,问题也就出在这,实际上是由于“操作不统一”造成的。
当对UTF-8编码的文件进行操作时,如果要把读取的内容当作文本内容来处理,最好先对BOM进行一些处理,这个问题在PHP6中得到了解决(可以设置文本/二进制读取模式),有兴趣的朋友可以自己查找PHP6的手册。
一个较简单的解决方法:
[php] view plaincopy
- $dataStr = file_get_contents('test.txt');
- if (strpos($dataStr, "\xEF\xBB\xBF") === 0) {
- $dataStr = substr($dataStr, 3);
- }
- // 对$dataStr进行操作
- ?>
或者用正则来处理:
[php] view plaincopy
- $dataStr = file_get_contents('test.txt');
- if (preg_match('/^\xEF\xBB\xBF/', $dataStr)) {
- $dataStr = substr($dataStr, 3);
- }
- // 对$dataStr进行操作
- ?>
什么是BOM?
BOM是Byte Order Mark的缩写,即字节顺序标记,它是插入到UTF-8,UTF-16或UTF-32编码的Unicode文件开头的特殊标记,用来标识Unicode文件的编码类型。
几种编码对应的BOM:
EF BB BF UTF-8
FE FF UTF-16 (big-endian)
FF FE UTF-16 (little-endian)
00 00 FE FF UTF-32 (big-endian)
FF FE 00 00 UTF-32 (little-endian)
对于UTF-8编码的文件而言,BOM标记是可有可无的,Windows自带的记事本文件在保存为UTF-8编码时,会自动加上BOM,现在一些编辑软件,可以在保存为UTF-8编码时可以选择是否带BOM保存。
对于PHP文件,在使用UTF-8编码时,最好都不要BOM保存。因为当你使用include/require/include_once/require_once这些函数去包含一个带BOM的文件时,你得到的网页,在某些兼容性不是很好的浏览器下,你会发现你的网页的实际显示效果跟预期的有细微的差别。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The differences between php5 and php8 are in terms of performance, language structure, type system, error handling, asynchronous programming, standard library functions and security. Detailed introduction: 1. Performance improvement. Compared with PHP5, PHP8 has a huge improvement in performance. PHP8 introduces a JIT compiler, which can compile and optimize some high-frequency execution codes, thereby improving the running speed; 2. Improved language structure, PHP8 introduces some new language structures and functions. PHP8 supports named parameters, allowing developers to pass parameter names instead of parameter order, etc.

Use Java's File.length() function to get the size of a file. File size is a very common requirement when dealing with file operations. Java provides a very convenient way to get the size of a file, that is, using the length() method of the File class. . This article will introduce how to use this method to get the size of a file and give corresponding code examples. First, we need to create a File object to represent the file we want to get the size of. Here is how to create a File object: Filef

To learn more about open source, please visit: 51CTO Hongmeng Developer Community https://ost.51cto.com Running environment DAYU200:4.0.10.16SDK: 4.0.10.15IDE: 4.0.600 1. To create an application, click File- >newFile->CreateProgect. Select template: [OpenHarmony] EmptyAbility: Fill in the project name, shici, application package name com.nut.shici, and application storage location XXX (no Chinese, special characters, or spaces). CompileSDK10, Model: Stage. Device

How to convert php blob to file: 1. Create a php sample file; 2. Through "function blobToFile(blob) {return new File([blob], 'screenshot.png', { type: 'image/jpeg' })} ” method can be used to convert Blob to File.

Use Java's File.renameTo() function to rename files. In Java programming, we often need to rename files. Java provides the File class to handle file operations, and its renameTo() function can easily rename files. This article will introduce how to use Java's File.renameTo() function to rename files and provide corresponding code examples. The File.renameTo() function is a method of the File class.

If you are an IT administrator or technology expert, you must be aware of the importance of automation. Especially for Windows users, Microsoft PowerShell is one of the best automation tools. Microsoft offers a variety of tools for your automation needs, without the need to install third-party applications. This guide will detail how to leverage PowerShell to automate tasks. What is a PowerShell script? If you have experience using PowerShell, you may have used commands to configure your operating system. A script is a collection of these commands in a .ps1 file. .ps1 files contain scripts executed by PowerShell, such as basic Get-Help

Use java's File.getParent() function to get the parent path of a file. In Java programming, we often need to operate files and folders. Sometimes, we need to get the parent path of a file, which is the path of the folder where the file is located. Java's File class provides the getParent() method to obtain the parent path of a file or folder. The File class is Java's abstract representation of files and folders. It provides a series of methods for operating files and folders. Among them, get

Use java's File.getParentFile() function to get the parent directory of a file. In Java programming, we often need to operate files and folders. When we need to get the parent directory of a file, we can use the File.getParentFile() function provided by Java. This article explains how to use this function and provides code examples. File class in Java is the main class used to operate files and folders. It provides many methods to obtain and manipulate file properties
