this is my code
$string="رستوران";
$arr = str_split($string);
var_dump($arr);
echo '<br>';
after running:
array (size=14)
0 => string '�' (length=1)
1 => string '�' (length=1)
2 => string '�' (length=1)
3 => string '�' (length=1)
.
.
.
please help me.thanks a lot
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] => ا
)
Use the following
$arr = str_split_unicode($string);
Create a function to handle this new call
function str_split_unicode($str, $l = 0) {
if ($l > 0) {
$ret = array();
$len = mb_strlen($str, "UTF-8");
for ($i = 0; $i < $len; $i += $l) {
$ret[] = mb_substr($str, $i, $l, "UTF-8");
}
return $ret;
}
return preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY);
}
The problem is not concerned with the str_split()
function. You have to change the character encoding of the file to one of the Unicode choices (UTF-8 is the best). The characters you are using are not suppported by the ANSI and ASCII encodings.
From PHP 7.4 you can use the mb_str_split() function:
$string="رستوران";
mb_str_split($string)