gpt4 book ai didi

php - PHP PCRE 中的\x 是什么意思?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:35:44 24 4
gpt4 key购买 nike

来自 the manual :

After \x, up to two hexadecimal digits are read (letters can be in upper or lower case). In UTF-8 mode, \x{...} is allowed, where the contents of the braces is a string of hexadecimal digits. It is interpreted as a UTF-8 character whose code number is the given hexadecimal number. The original hexadecimal escape sequence, \xhh, matches a two-byte UTF-8 character if the value is greater than 127.

那么这是什么意思呢?

“ä”的代码点是 E4,而 UTF-8 表示是 C3A4,但两者都不匹配:

$t = 'ä'; // same as "\xC3\xA4";

preg_match('/\\xC3A4/u', $t); // doesn't match
preg_match('/\\x00E4/u', $t); // doesn't match

当我给出代码点时,花括号确实匹配:

preg_match('/\\x{00E4}/u', $t); // matches

最佳答案

语法是一种按值指定字符的方法:

  • \xAB 指定 0-FF 范围内的代码点。
  • \x{ABCD} 指定 0-FFFF 范围内的代码点。

手册中的指示措辞有点困惑,也许是为了准确。字符值 128-255(和一些)在 UTF-8 中被编码为 2 字节。因此,unicode 正则表达式将匹配 7 位干净的 ASCII,但不会匹配使用所述范围内的值的不同编码/代码页(即 CP437)。该手册以迂回的方式说 unicode 正则表达式仅适用于正确编码的输入。然而;

这并不意味着 \xABCD解析\x{ABCD}(一个字符)。它被解析为 \xAB(一个字符)和 then CD(两个字符)1。大括号解决了这个解析歧义问题:

After \x, up to two hexadecimal digits are read .. In UTF-8 mode, \x{...} is allowed ..

一些其他语言使用 \u 而不是 \x 来获得更长的形式。


1 认为这匹配:

preg_match('/\xC3A4/u', "\xC3" . "A4");

关于php - PHP PCRE 中的\x 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18522661/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com