【问题标题】:PHP not Unicode Support Disadvantages within PCREPHP 不是 Unicode 支持 PCRE 中的缺点
【发布时间】:2011-07-13 19:58:55
【问题描述】:

例如:

<!-- All the characters are going to be converted into a Hex values depending the encoding used -->

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <!-- It Just interpret the Hex values that are going to be displayed -->


<?php

/* PHP Strings are bytestream */
/* PHP treat the strings as a Hex values from the econding used */

$string="€"; // Hex value from the Encoding Method(UTF-8). [U+20AC][E2|82|AC]
if(preg_match('/\xE2\x82\xAC/',$string,$m)){
    echo "Match<br>";
    print_r($m);
    }
else{
    echo "Don't Match";
    }

?>

只要您使用正确的字节序列来匹配 Unicode 字符。 不需要使用Unicode Support吗?

还是我想错了?

【问题讨论】:

    标签: php regex unicode utf-8 pcre


    【解决方案1】:

    对于特定的匹配,您不需要 Unicode 支持。任何简单的直接字符串匹配都适用于两个 UTF-8 字符串——这是 UTF-8 精心设计的特点——但如果你只需要一个直接字符串匹配,你就不会使用正则表达式:对于你的例子,你最好使用strpos

    在没有 Unicode 支持的情况下,许多其他正则表达式功能会出现意外行为。例如:

    /€*/
    

    支持 Unicode,即多个 € 符号 (\xE2\x82\xAC\xE2\x82\xAC\xE2\x82\xAC...)。没有它,这是 € 符号的前两个字节,然后是任意数量的 0xAC 字节 (\xE2\x82\xAC\xAC\xAC\xAC...),因此它匹配的唯一有效 UTF-8 序列将是单个 €。

    /[x€]/
    

    支持 Unicode,匹配 x 或欧元。没有 Unicode 支持,匹配 x 或字节 0xE2 或字节 0x82 或字节 0xAC。

    等等。

    【讨论】:

      猜你喜欢
      • 2011-08-15
      • 2013-02-24
      • 1970-01-01
      • 1970-01-01
      • 2011-10-03
      • 2018-11-03
      • 2013-01-19
      • 1970-01-01
      相关资源
      最近更新 更多