


After php reads the csv file, the solution to the problem of uft8 bom causing the display on the page_PHP tutorial
date.csv:
"ID" "NAME" "EMAIL"
"1" "Xiao Ming" "xm@163.com"
"2" "Xiaodong " "xd@sina.com"
"3" "小少" "shaozi@hotmai.com"
Read this csv file
$handle=fopen('date.csv','r');
while($data=fgetcsv($handle,10000,"/t"))
{
echo "$data[0]"."$data[1]"."$data[2]";
}
?>
After being read and displayed on the page, it looks like this:
"ID" NAME EMAIL
1 Xiaomingxm@163.com
2 Xiaodongxd@sina. com
3 Xiaoshao shaozi@hotmai.com
The field surround character of the fgetcsv function is double quotes by default.
Why when I read it out, other fields are all fine, but the ID is still wrapped in double quotes. ?
After checking online, it turns out that the utf8 encoded BOM cannot be recognized under PHP.
The following is the information I found:
There is a concept of BOM in the Unicode specification. BOM - Byte Order Mark, which is the byte order mark. Find a description about BOM here
:
There is a character called "ZERO WIDTH NO-BREAK SPACE" in UCS encoding, and its encoding is FEFF. FFFE is a character that does not exist in UCS, so it should not appear in actual transmission. The UCS specification recommends that we transmit the characters "ZERO WIDTH NO-BREAK SPACE" before transmitting the byte stream. In this way, if the receiver receives FEFF, it indicates that the byte stream is Big-Endian; if it receives FFFE, it indicates that the byte stream is Little-Endian. Therefore the character "ZERO WIDTH NO-BREAK SPACE" is also called BOM.
UTF-8 does not require a BOM to indicate the byte order, but can use the BOM to indicate the encoding method. The UTF-8 encoding of the character "ZERO WIDTH NO-BREAK SPACE" is EF BB BF. So if the receiver receives a byte stream starting with EF BB BF, it knows that it is UTF-8 encoded.
Windows uses BOM to mark the encoding method of text files.
In addition, the unicode website's
FAQ-BOM
introduces BOM in detail. The official natural authority is only in English, which seems more laborious.
In UTF-8 encoded files, the BOM occupies three bytes. If you use Notepad to save a text file as UTF-8 encoding, open the file with UE and switch to the hexadecimal editing state, you can see the FFFE at the beginning. This is a good way to identify UTF-8 encoded files. The software uses BOM to identify whether the file is UTF-8 encoded. Many software also require that the read file must have BOM. However, there are still many software that cannot recognize BOM. When I was studying Firefox, I knew that in early versions of Firefox, extensions could not have BOMs, but versions after Firefox 1.5 have begun to support BOMs. Now I discovered that PHP does not support BOM either.
PHP did not consider the BOM issue when it was designed, which means that it will not ignore the three characters of the BOM at the beginning of the UTF-8 encoded file. Because you must convert->UTF-8 to ASCII, or select ASCII encoding in Save As. If it is a line ending in DOS format, you can open it with Notepad, click Save As, and select ASCII encoding. If it contains Chinese characters, you can use UE's save as function and select "UTF-8 without BOM". Please refer to the picture below:
According to Bo-Blog's wiki: Editplus needs to be saved as gb first, and then saved as UTF-8. But be careful when doing this, as all characters not included in the GBK encoding will be lost. If there are some non-Chinese characters in the file, it is better not to use this method. (From this small aspect, UE-UltraEdite-32 is indeed much better than Editplus, Editplus is too lightweight)
In addition, I found a way, which is to use the file editor provided by WordPress. This method is unrestricted and does not require downloading a special editor. After all, everyone is using WordPress. First, open the write permission of the file you want to edit in ftp, then enter the WordPress backend->Management->File Editor, enter the path of the file you want to edit, and click Edit File. In the editing interface that is displayed, you can't see the first three characters, but it doesn't matter. Position the cursor in front of the first character of the entire file and press the Backspace key. OK, click update file, refresh it in ftp, you can see that the file is 3 bytes smaller, and you're done.
Finally, this is a big problem. For those who want to write their own plug-ins, edit other people’s plug-ins for their own use, or need to modify templates (I think everyone needs this), it is best to understand the above. knowledge to avoid being overwhelmed when problems arise.

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
