问题
I'm trying to extract the special characters (out of a predefined pattern) from a string, but when that string begins with an inverted question mark, "matches" returns first two characters, including a not special one. Eg.:
$string = '¿hola?';
$string2 = mb_convert_encoding($string, 'UTF-8');
$regex = mb_convert_encoding('/[a-zäáàëéèíìöóòúùñç]/', 'UTF-8');
if(preg_match($regex, $string2, $matches, PREG_OFFSET_CAPTURE))
{
//--> We pick the special characters into "$resultado1":
$resultado1 = mb_substr($string, 0, $matches[0][1],'UTF-8');
return $resultado1;
}
In this example, the function returns "¿h", but "¿" was expected... I can't figure out the problem...
回答1:
Try to use the flag "u" (as documented on this page) in your regex: /[a-zäáàëéèíìöóòúùñç]/u
And prefer saving your files in UTF-8 rather than using mb_convert_encoding on static strings.
来源:https://stackoverflow.com/questions/10302582/preg-match-is-matching-two-characters-when-it-should-only-match-one