phpunicodefarsi

I have some trouble with str_split, it doesn't work correctly with my language


I've a piece of code like this:

$string="رستوران";
$arr = str_split($string);
var_dump($arr);
echo '<br>';

that the result is:

array (size=14)
   0 => string '�' (length=1)
   1 => string '�' (length=1)
   2 => string '�' (length=1)
...

but I expect to have result like this:

array (size=14)
   0 => string 'ر' (length=1)
   1 => string 'س' (length=1)
   2 => string 'ت' (length=1)
...

Do you have any suggestions for achieving my expected result?


Solution

  • You must use Multibyte String Functions for manipulating persian string. You can use preg_split for your porpuse.

    print_r(preg_split('//u', "رستوران ها", null, PREG_SPLIT_NO_EMPTY));
    Output:
    Array
    (
        [0] => ر
        [1] => س
        [2] => ت
        [3] => و
        [4] => ر
        [5] => ا
        [6] => ن
        [7] =>  
        [8] => ه
        [9] => ا
    )