json_encode returns false when dealing with multibyte substring json_encode returns false when dealing with multibyte substring json json

json_encode returns false when dealing with multibyte substring


Simply pass the utf-8 encoding as the fourth parameter of the mb_substr() and you are good to go.

$a = mb_substr($s, 0, 10,'utf-8');echo $a; // に搭載されるようになecho json_encode($a); // "\u306b\u642d\u8f09\u3055\u308c\u308b\u3088\u3046\u306b\u306a"

Demonstration