note 79273 deleted from function.preg-match by tularis
| From: | tularis@php.net | Date: | Thu, 03 Jul 2008 21:38:12 +0000 |
| Subject: | note 79273 deleted from function.preg-match by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-141143@lists.php.net to get a copy of this message | ||
Note Submitter: boyan7640 at gmail dot com
----
If you try to find the offset when searching in UTF-8 string (containing multibyte characters, like
cyrillic characters) with preg_match, using the PREG_OFFSET_CAPTURE flag, you may have different
result from what you expected.
First of all you must compiled PHP with Multibyte Support (mbstring). Then you must configure to use
Multibyte Support functions (mb_*) or turn on some php Runtime Configurations (php.ini, apache vhost
conf file, .htaccess or somewhere else):
php_value default_charset UTF-8
php_value mbstring.func_overload 7
php_value mbstring.internal_encoding UTF-8
php_value mbstring.detect_order UTF-8
When using preg_match with PREG_OFFSET_CAPTURE flag and UTF-8 string the function will count bytes
and NOT characters, so 2 bytes but NOT 1 character for some multibyte character. That's way the
offset will be more than what you expected.
My simple solution is using mb_strpos:
...
preg_match($pattern, $found_text, $matches, PREG_OFFSET_CAPTURE);
// This will convert $matches[0][1] multibyte byte length to multibyte character length (UTF-8)
$matches[0][1] = mb_strpos($found_text, $matches[0][0]);
...
P.S. The $pattern variable must use "/u" switch for Unicode!!!
-------------------------------------------------
PHP Version 5.2.4
Multibyte regex (oniguruma) version 4.4.4
-------------------------------------------------