Paulius
Paulius

Reputation: 99

PHP preg_split utf8 characters

Have problem with preg split and utf. This is code:

$original['words'] = preg_split("/[\s]+/", $original['text']);
print_r($original);

This is answer:

Array
(

    [text] => Šios baterijos kaista
    [words] => Array
        (
            [0] => �
            [1] => ios
            [2] => baterijos
            [3] => kaista

This code is runing in CakePHP framework. Make a notice that [text] is showed correctly before words and is messed in split progress. By the way, I tried using these one:

mb_internal_encoding( 'UTF-8'); 
mb_regex_encoding( 'UTF-8');  
ini_set('default_charset','utf-8');

None helped. Thank you.

Upvotes: 5

Views: 4567

Answers (2)

AaronJ
AaronJ

Reputation: 1170

$original = mb_split("[\s]+", 'Šios baterijos kaista');
print_r($original);

Result:

Array
(
    [0] => Šios
    [1] => baterijos
    [2] => kaista
)

Note:

1) Don't forget to remove the leading and trailing '/' from the regex pattern when using mb_split.

2) Only works if the mbstring extension is enabled.

Upvotes: 0

Jon
Jon

Reputation: 437406

You need to enable utf-8 mode for preg_split by adding the u modifier to the regular expression:

preg_split("/[\s]+/u", $original['text']);

The configuration directives you mention as part of trying to find a solution play no role here.

Upvotes: 19

Related Questions