Table of Contents
不同编码的BOM表示
Home php教程 php手册 About字符编码

About字符编码

Jun 06, 2016 pm 08:12 PM
character Open program coding notebook

打开"记事本"程序Notepad.exe,新建一个文本文件,内容就是一个"严"字,依次采用ANSI,Unicode,Unicode big endian 和 UTF-8编码方式保存。 然后,用文本编辑软件UltraEdit中的"十六进制功能",观察该文件的内部编码方式。 1)ANSI:文件的编码就是两个字节

打开"记事本"程序Notepad.exe,新建一个文本文件,内容就是一个"严"字,依次采用ANSI,Unicode,Unicode big endian 和 UTF-8编码方式保存。

然后,用文本编辑软件UltraEdit中的"十六进制功能",观察该文件的内部编码方式。

1)ANSI:文件的编码就是两个字节"D1 CF",这正是"严"的GB2312编码,这也暗示GB2312是采用大头方式存储的。

2)Unicode:编码是四个字节"FF FE 25 4E",其中"FF FE"表明是小头方式存储,真正的编码是4E25。

3)Unicode big endian:编码是四个字节"FE FF 4E 25",其中"FE FF"表明是大头方式存储。

4)UTF-8:编码是六个字节"EF BB BF E4 B8 A5",前三个字节"EF BB BF"表示这是UTF-8编码,后三个"E4B8A5"就是"严"的具体编码,它的存储顺序与编码顺序是一致的。

UTF-8编码的文件中,BOM占三个字节。如果用记事本把一个文本文件另存为UTF-8编码方式的话,用UE打开这个文件,切换到十六进制编辑状态就可以看到开头的FFFE了。这是个标识UTF-8编码文件的好办法,软件通过BOM来识别这个文件是否是UTF-8编码,很多软件还要求读入的文件必须带BOM。可是,还是有很多软件不能识别BOM。

PHP在设计时就没有考虑BOM的问题,也就是说他不会忽略UTF-8编码的文件开头BOM的那三个字符。

由于必须在在Bo-Blog的wiki看到,同样使用PHP的Bo-Blog也一样受到BOM的困扰。其中有提到另一个麻烦:“受COOKIE送出机制的限制,在这些文件开头已经有BOM的文件中,COOKIE无法送出(因为在COOKIE送出前PHP已经送出了文件头),所以登入和登出功能失效。一切依赖COOKIE、SESSION实现的功能全部无效。”这个应该就是Wordpress后台出现空白页面的原因了,因为任何一个被执行的文件包含了BOM,这三个字符都将被送出,导致依赖cookies和session的功能失效。

解决的办法嘛,如果只包含英文字符(或者说ASCII编码内的字符),就把文件存成ASCII码方式吧。用UE等编辑器的话,点文件->转换->UTF-8转ASCII,或者在另存为里选择ASCII编码。如果是DOS格式的行尾符,可以用记事本打开,点另存为,选ASCII编码。如果包含中文字符的话,可以用UE的另存为功能,选择“UTF-8 无 BOM”即可。

当然在Python程序中处理的时候遇到这种情况改怎么处理呢?

方法一:直接将源文件的格式修改一下,将其保存为UTF-8即可,但是很多时候传过来的源文件是没法修改的,这样就行不通了。

方法二:替换,就是将其中的\xef\xbb\xbf替换为空就可以了。

VIM 十六进制和文本模式切换

使用vim打开文件后,使用命令

:%!xxd 使用十六进制显示;
:%!xxd -r 返回文本显示。
Copy after login

只有十六进制部分的修改才会被采用。右边可打印文本部分的修改忽略不计。

vim以二进制打开文件

vim -b binfile
Copy after login

在 Vim 中将文件保为无 BOM 的 UTF-8 格式

查看文件格式

通常我们需要先查看文件格式是否与预期的一样,再根据结果决定是否需要修改(当然你也可以在不知道原格式的情况下直接修改),下面分别给出了查看文件编码和是否带有 BOM 的命令。

# 查看文件编码。
set fenc?
# 查看是否带 BOM。
set bomb?
Copy after login

修改文件格式

# 设置为 UTF-8 编码。
set fenc=utf-8
# 设置为无 BOM,如需设置为带 BOM 则使用 "set bomb"。
set nobomb
# 加上BOM标记
set bomb
Copy after login

使用linux命令删除UTF-8编码中的BOM

shell> grep -r -I -l $'^\xEF\xBB\xBF' /path | xargs sed -i 's/^\xEF\xBB\xBF//;q'
or
shell> grep -r -I -l $'^\xEF\xBB\xBF' /path | xargs sed -i 's/^\xEF\xBB\xBF//g'
or
shell> tail -c +4 old_file > new_file
Copy after login

如果使用SVN提交代码的话,可以在pre-commit钩子里加上相关代码用以杜绝BOM。

#!/bin/bash
REPOS="$1"
TXN="$2"
SVNLOOK=/usr/bin/svnlook
for FILE in $($SVNLOOK changed -t "$TXN" "$REPOS" | awk '/^[AU]/ {print $NF}'); do
    if $SVNLOOK cat -t "$TXN" "$REPOS" "$FILE" | grep -q $'^\xEF\xBB\xBF'; then
        echo "Byte Order Mark be found in $FILE" 1>&2
        exit 1
    fi
done
Copy after login

不含 BOM 的 UTF-8 才是标准形式!
带 BOM 的 UTF-8 就是赤裸裸的流氓!!!

不同编码的BOM表示

编码 十六进制表示
UTF-8 EF BB BF
UTF-16-BE FE FF
UTF-16-LE FF FE
UTF-32-BE 00 00 FE FF
UTF-32-LE FF FE 00 00
UTF-7 2B 2F 76和以下的一个字节:[ 38 39 2B 2F ]
en:UTF-1 F7 64 4C
en:UTF-EBCIC DD 73 66 73
en:Standard Compression Scheme for Unicode 0E FE FF
en:BOCU-1 FB EE 28及可能跟随着FF
GB-18030 84 31 95 33

vim 将文件从dos格式转换到unix格式

:set fileformat=unix
:w
Copy after login

VIM 状态栏显示 文件格式带bom
Show fileencoding and bomb in the status line

http://vim.wikia.com/wiki/Show_fileencoding_and_bomb_in_the_status_line

比如 [latin1], [iso-8859-15], [utf-8,B], etc.

if has("statusline")
 set statusline=%
<p>其实有用的就是这段:</p>
<pre class="brush:php;toolbar:false">
%{\"[\".(&fenc==\"\"?&enc:&fenc).((exists(\"+bomb\")\ &&\ &bomb)?\",B\":\"\").\"]\ \"}
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to make Google Maps the default map in iPhone How to make Google Maps the default map in iPhone Apr 17, 2024 pm 07:34 PM

The default map on the iPhone is Maps, Apple's proprietary geolocation provider. Although the map is getting better, it doesn't work well outside the United States. It has nothing to offer compared to Google Maps. In this article, we discuss the feasible steps to use Google Maps to become the default map on your iPhone. How to Make Google Maps the Default Map in iPhone Setting Google Maps as the default map app on your phone is easier than you think. Follow the steps below – Prerequisite steps – You must have Gmail installed on your phone. Step 1 – Open the AppStore. Step 2 – Search for “Gmail”. Step 3 – Click next to Gmail app

How to open iso file on mobile phone How to open iso file on mobile phone Feb 24, 2024 pm 05:33 PM

With the development of technology, more and more people are now starting to use ISO files. An ISO file is a format of a disc file that usually contains all the contents of the disc, including operating system, software, games, etc. Opening an ISO file on your computer is relatively simple, just use virtual optical drive software. However, when we want to open ISO files on our mobile phones, it may be more troublesome. Well, this article will introduce some methods for you to easily open ISO files on your phone. 1. Using the virtual optical drive application First, we can install

Can I open an XML file using PowerPoint? Can I open an XML file using PowerPoint? Feb 19, 2024 pm 09:06 PM

Can XML files be opened with PPT? XML, Extensible Markup Language (Extensible Markup Language), is a universal markup language that is widely used in data exchange and data storage. Compared with HTML, XML is more flexible and can define its own tags and data structures, making the storage and exchange of data more convenient and unified. PPT, or PowerPoint, is a software developed by Microsoft for creating presentations. It provides a comprehensive way of

Share the method to solve the problem that PyCharm cannot be opened Share the method to solve the problem that PyCharm cannot be opened Feb 22, 2024 am 09:03 AM

Title: How to solve the problem that PyCharm cannot be opened. PyCharm is a powerful Python integrated development environment, but sometimes we may encounter the problem that PyCharm cannot be opened. In this article, we'll share some common workarounds and provide specific code examples. Hope this helps those who encounter this problem. Method 1: Clear the cache Sometimes PyCharm’s cache files may cause the program to fail to open normally. We can try clearing the cache to solve this problem. Tool

How to open torrent files How to open torrent files Feb 24, 2024 pm 05:20 PM

Torrent seed files can be opened with software, so what software can be used to open them? Users need to open it with download tools such as Thunder, BitComet, qBittorrent, etc., and then download. This introduction to how to open torrent files can tell you the specific content. The following is a detailed introduction, take a look! How to open a torrent seed file? Answer: Use Thunder, BitComet, qBittorrent and other download tools to open it. Detailed introduction: 1. Torrent is a BT seed file that can be opened with various download software. 2. The more people download this software, the faster the download speed will be. 3. In order to ensure accuracy, the software will calculate Ha every time it downloads a block.

Where can I open the Douyin mini game? Two ways to open Douyin mini games Where can I open the Douyin mini game? Two ways to open Douyin mini games Mar 15, 2024 am 09:30 AM

Douyin is a popular short video social application. Users can not only watch and share various types of short video content, but also provide a series of mini games for you to play. So where can I get into Douyin mini-games? Where is the entrance to Douyin mini game? Let’s take a look at the detailed tutorial below. Method 1: Open the mini program 1. Click the My option. After entering the homepage of Douyin, click the My option to enter. 2. Click the three horizontal lines. After entering the My interface, click the three horizontal lines button above. 3. Click on the mini program. After opening the three horizontal lines option, click on the mini program inside. 4. Click on the Douyin Mini Game. After entering the mini program interface, click on the Douyin Mini Game option to open it. Method 2: Search and open 1. Click the magnifying glass to enter

Clock app missing in iPhone: How to fix it Clock app missing in iPhone: How to fix it May 03, 2024 pm 09:19 PM

Is the clock app missing from your phone? The date and time will still appear on your iPhone's status bar. However, without the Clock app, you won’t be able to use world clock, stopwatch, alarm clock, and many other features. Therefore, fixing missing clock app should be at the top of your to-do list. These solutions can help you resolve this issue. Fix 1 – Place the Clock App If you mistakenly removed the Clock app from your home screen, you can put the Clock app back in its place. Step 1 – Unlock your iPhone and start swiping to the left until you reach the App Library page. Step 2 – Next, search for “clock” in the search box. Step 3 – When you see “Clock” below in the search results, press and hold it and

How to open caj file How to open caj file on computer How to open caj file How to open caj file on computer Feb 22, 2024 pm 04:37 PM

Select the file in the word converter and click Start Conversion to open it. Analysis 1. Open the CAJ to Word converter and select CAJ to Word on the left side of the software. 2Then select the file you want to convert and click Open. 3Finally, click Start Conversion and you can open the caj file in Word. Supplement: What is the format of caj? 1CAJ is a format of electronic journals. You can use the CAJ full-text browser to read. Supports CAJ, NH, KDH and PDF format files of China Journal Network. You can read the original text of China Journal Network online, or you can read the full text of China Journal Network downloaded to your local hard drive. Its printing effect can be consistent with the original display. Summary/Notes CAJ is a Chinese academic journal

See all articles