为什么substr()错误计算阿拉伯字符串?

I have a php variable that is contained 9 Arabic character. But when I set 9 in the substr(), the output is 4 character. Why ? How can I fix it ?

here is my demo.

and here is my codes:

$str = 'سلام جهان'; 
// the number of character: 9 character {س|ل|ا|م| |ج|ه|ا|ن}

echo substr($str, 0, 9);

// output: سلام

?>

Try using the multi-byte equivalent:

mb_internal_encoding("UTF-8");
$string = "سلام جهان";
$mystring = mb_substr($string,5,1);
echo $mystring;

You can also set the flag using my_substr:

mb_substr($string, 5,1, "utf-8");