preg_match_all(PHP)中的UTF-8字符 [英] UTF-8 characters in preg_match_all (PHP)

查看:168
本文介绍了preg_match_all(PHP)中的UTF-8字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有preg_match_all('/[aäeëioöuáéíóú]/u', $in, $out, PREG_OFFSET_CAPTURE);

如果$in = 'hëllo' $out是:

array(1) {
[0]=>
  array(2) {
  [0]=>
    array(2) {
      [0]=>
      string(2) "ë"
  [1]=>
  int(1)
}
[1]=>
array(2) {
  [0]=>
  string(1) "o"
  [1]=>
  int(5)
  }
}
}

o的位置应为4.我已在线阅读有关此问题的信息(ë被计为2).有解决方案吗?我见过mb_substr和类似的内容,但是preg_match_all是否有类似的内容?

The position of o should be 4. I've read about this problem online (the ë gets counted as 2). Is there a solution for this? I've seen mb_substr and similar, but is there something like this for preg_match_all?

相关种类:它们在Python中是否等同于preg_match_all? (返回匹配项及其在字符串中的位置)

Kind of related: Is their an equivalent of preg_match_all in Python? (Returning an array of matches with their position in the string)

推荐答案

PHP不能很好地支持unicode,因此许多字符串函数(包括preg_ *)仍会计数字节而不是字符.

PHP doesn't support unicode very well, so a lot of string functions, including preg_*, still count bytes instead of characters.

我尝试通过对字符串进行编码和解码来找到解决方案,但最终所有归结于preg_match_all函数.

I tried finding a solution by encoding and decoding strings, but ultimately it all came down to the preg_match_all function.

关于python事物:python regex matchobject默认包含mo.start()和mo.end()的匹配位置.请参阅: http://docs.python. org/library/re.html#finding-all-adverbs-and-their-positions

About the python thing: a python regex matchobject contains the match position by default mo.start() and mo.end(). See: http://docs.python.org/library/re.html#finding-all-adverbs-and-their-positions

这篇关于preg_match_all(PHP)中的UTF-8字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆