preg_match is matching two characters when it should only match one

我的未来我决定 提交于 2020-01-16 12:45:35

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!