preg_match и UTF-8 в PHP

Что-либо программа с деревом или структурами данных графика будет, вероятно, иметь некоторую рекурсию.

33
задан JW. 12 November 2009 в 20:55
поделиться

2 ответа

Looks like this is a "feature", see http://bugs.php.net/bug.php?id=37391

'u' switch only makes sense for pcre, PHP itself is unaware of it.

From PHP's point of view, strings are byte sequences and returning byte offset seems logical (i don't say "correct").

20
ответ дан 27 November 2019 в 17:41
поделиться

Although the u modifier makes both the pattern and subject be interpreted as UTF-8, the captured offsets are still counted in bytes.

You can use mb_strlen to get the length in UTF-8 characters rather than bytes:

$str = "\xC2\xA1Hola!";
preg_match('/H/u', $str, $a_matches, PREG_OFFSET_CAPTURE);
echo mb_strlen(substr($str, 0, $a_matches[0][1]));
39
ответ дан 27 November 2019 в 17:41
поделиться
Другие вопросы по тегам:

Похожие вопросы: