- Shell :#! /Bin/sh: No such file or directory
- PHP: Warning: Cannot modify header information-headers already sent
Before discussing in detail the problem of BOM detection and deletion in UTF-8 coding, we may try to warm up with an example:
- Shell> curl-s http://phone.jbxue.com/| head-1 | sed-n l
- \ 357 \ 273 \ 277// EN "" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> \ r$
As shown above, the first three bytes are 357, 273, and 277, which are the BOM of the octal component.
- Shell> curl-s http://phone.jbxue.com/| head-1 | hexdump-C
- 00000000 ef bb bf 3c 21 44 4f 43 54 59 50 45 20 68 74 6d |...00000010 6c 20 50 55 42 4c 49 43 20 22 2d 2f 57 33 43 | l PUBLIC "-// W3C |
- 00000020 2f 2f 44 54 44 20 58 48 54 4d 4c 20 31 2e 30 20 | // dtd xhtml 1.0 |
- 00000030 54 72 61 6e 73 69 74 69 6f 6e 61 6c 2f 2f 45 4e | Transitional // EN |
- 00000040 22 20 22 68 74 70 3a 2f 2f 77 77 77 2e 77 33 | "" http: // www. w3 |
- 00000050 2e 6f 72 67 2f 54 52 2f 78 68 74 6d 6c 31 2f 44 |. org/TR/xhtml1/D |
- 00000060 54 44 2f 78 68 74 6d 6c 31 2d 74 72 61 6e 73 69 | TD/xhtml1-transi |
- 00000070 74 69 6f 6e 61 6c 2e 64 74 64 22 3e 0d 0a | tional. dtd ">... |
As shown above, the first three bytes are EF, BB, and BF, which are the hexadecimal BOM. Note: When a third-party website page is used, examples cannot be always available. In actual project development, may face hundreds of thousands of text files, if a few files mixed into the BOM, it is difficult to notice, if there is no BOM UTF-8 text file example, you can use vi to write a few, the relevant commands are as follows: # set the UTF-8 encoding: set fileencoding = UTF-8 # Add BOM: set bomb # delete BOM: set nobomb # Query BOM: set bomb? Inspect BOM in UTF-8 code
- Shell> grep-r-I-l $ '^ \ xEF \ xBB \ xBF'/path
Delete BOM from UTF-8 encoding
- Shell> grep-r-I-l $ '^ \ xEF \ xBB \ xBF'/path | xargs sed-I's/^ \ xEF \ xBB \ xBF //; q'
Recommendation: If SVN is used, you can add relevant code to the pre-commit hook to prevent BOM.
#! /Bin/sh
REPOS = "$1"
- TXN = "$2"
SVNLOOK =/usr/bin/svnlook
FILES = '$ SVNLOOK changed-t "$ TXN" "$ REPOS" | awk'/^ [UA]/{print $2 }''
For FILE in $ FILES; 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
Articles you may be interested in: php instances: check and clear BOM information at the beginning of the file implementation code Php remove bom header information in batches php remove bom header code share simple example of BOM data on the PHP filter page check whether the Php file has a bom header how to check the bom in php files in batches by code and how to clear the bom function in php files. how to solve style disorder caused by BOM in UTF-8 |