Detection and deletion of BOM in UTF-8 coding

Source: Internet
Author: User
Detection and deletion of BOM in UTF-8 coding

  1. Shell :#! /Bin/sh: No such file or directory
  2. 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:

  1. Shell> curl-s http://phone.jbxue.com/| head-1 | sed-n l
  2. \ 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.

  1. Shell> curl-s http://phone.jbxue.com/| head-1 | hexdump-C
  2. 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 |
  3. 00000020 2f 2f 44 54 44 20 58 48 54 4d 4c 20 31 2e 30 20 | // dtd xhtml 1.0 |
  4. 00000030 54 72 61 6e 73 69 74 69 6f 6e 61 6c 2f 2f 45 4e | Transitional // EN |
  5. 00000040 22 20 22 68 74 70 3a 2f 2f 77 77 77 2e 77 33 | "" http: // www. w3 |
  6. 00000050 2e 6f 72 67 2f 54 52 2f 78 68 74 6d 6c 31 2f 44 |. org/TR/xhtml1/D |
  7. 00000060 54 44 2f 78 68 74 6d 6c 31 2d 74 72 61 6e 73 69 | TD/xhtml1-transi |
  8. 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

  1. Shell> grep-r-I-l $ '^ \ xEF \ xBB \ xBF'/path

Delete BOM from UTF-8 encoding

  1. 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.

  1. #! /Bin/sh

  2. REPOS = "$1"

  3. TXN = "$2"

  4. SVNLOOK =/usr/bin/svnlook

  5. FILES = '$ SVNLOOK changed-t "$ TXN" "$ REPOS" | awk'/^ [UA]/{print $2 }''

  6. For FILE in $ FILES; do

  7. If $ SVNLOOK cat-t "$ TXN" "$ REPOS" "$ FILE" | grep-q $ '^ \ xEF \ xBB \ xBF'; then
  8. Echo "Byte Order Mark be found in $ FILE" 1> & 2
  9. Exit 1
  10. Fi
  11. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.