I’m trying to explode a string character by character but I’ve a trouble with special characters.
I’m currently using the following function:
<?php
$input = "Comment ça va?";
$array_input = str_split($input, 1);
print_r($array_input);
?>
Here’s the output:
Array (
[0] => C [1] => o [2] => m [3] => m [4] => e
[5] => n [6] => t [7] => [8] => � [9] => �
[10] => a [11] => [12] => v [13] => a [14] => ? )
I’ve the same issue with the line break:
Input:
“Hé!
Oui?”
Output:
Array ( [0] => H [1] => � [2] => � [3] => ! [4] =>
[5] => [6] => O [7] => u [8] => i [9] => ? )
Does anybody have a solution for this issue?
Many thanks.
str_splithas problems with Unicode strings.You can use the
umodifier inpreg_splitinsteadFor instance:
Will output