preg_match_all(PHP)中的UTF-8字符 [英] UTF-8 characters in preg_match_all (PHP)
问题描述
我有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屋!