Preg_match matches two characters when it must match only one

I'm trying to extract special characters (from a predefined pattern) from a string, but when this string starts with an inverted question mark, "match" returns the first two characters, including non-special ones. For instance:.

$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 expected "¿" ... I can not understand the problem ...

0
source share
1 answer

Try using the "u" flag (as described on this page ) in your regular expression: /[a-zäáàëéèíìöóòúùñç]/u

And prefer to save your files in UTF-8 rather than using mb_convert_encoding for static strings.

0
source

All Articles