PCRE中PHP不支持Unicode的缺点

PCRE中PHP不支持Unicode的缺点,php,regex,unicode,utf-8,pcre,Php,Regex,Unicode,Utf 8,Pcre,例如: <!-- 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 d

例如:

<!-- 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支持。任何简单的直接字符串匹配都适用于两个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

等等