Function of phppreg_replace modifier u

Source: Internet
Author: User
The phppreg_replace modifier u is described in

"This modifier opens an additional feature that is incompatible with perl. the mode string is considered to be UTF-8"

What do you mean? I don't know what I understand.

Utf8 is a variable-length code. a Chinese utf8 may be 2, 3, or 4 bytes in length.

Because of the special lines of this encoding, if you didn't tell php to reprocess the regular expression, utf8 php may be used. it's easy to confuse yourself. Chinese truncation causes garbled characters.

If this parameter is added, the system will analyze the first byte and determine the actual length.

Test ('/(\ D +) (\ d +) \ w/u'); function test ($ pre) {$ str = "hexagonal dinosaur: 12 "; $ a = preg_replace ($ pre, '$1 $2', $ str); echo $.'
';}

Reference others' utf8 explanations:

UTF, short for Unicode Text Format, stands for the Unicode Text Format. UTF encoding can be generated based on Unicode encoding. the conversion rules are as follows:
(1) first, convert the Unicode encoding to the binary form, so that a character corresponds to a 16-bit binary number.

(2) If the first nine digits of the Unicode 16-bit binary encoding are all 0, a single byte is used to represent the character. the first byte is "0 ", the remaining 7 digits are the same as the last 7 digits in the original encoding. For example, "\ u0034" (0000 0000 0011 0100), expressed in "34" (0011 0100) (the same as the original Unicode encoding, but the first byte of the original encoding is removed );


(3) If the first five digits of the Unicode 16-bit binary encoding are 0, two bytes are used to represent the character. the first byte starts with "110, the five digits after this byte are the same as the five digits after the first five zeros in the source encoding. the second byte starts with "10, the next six digits are the same as the remaining six digits in the source code. For example, "\ u025d" (0000 0010 0101 1101) is converted to "c99d" (1100 1001 1001 1101 );


(4) If the Unicode 16-bit binary code does not comply with the preceding two rules, the character is represented in three bytes. The first byte starts with "1110", and the last four digits are the same as the first four digits of the Source Encoding. the second byte starts with "10, the last six digits are the same as the next six digits of the original encoding. The third byte also starts with "10", and the last six digits are the same as the remaining six digits of the original encoding, in this way, the original 16-bit encoding is converted into three-byte 24-bit encoding, for example, "\ u9da7" (1001 1101 1010 ), convert to "e9b6a7" (1110 1001 1011 0110 1010 0111 ).

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.