json 如何在 laravel 5.2 json 响应中编码阿拉伯语


How json encode arabic in laravel 5.2 json response?

我的应用程序支持多种语言。

我的控制器,

 public function index(Request $request) {
    return DB::table('offer_types')
                    ->join('offer_type_details', 'offer_type_details.offer_types_id', '=', 'offer_types.id')
                    ->where('offer_type_details.languages_id', $request->language_id ? $request->language_id : 1)
                    ->get();
}

如果是阿拉伯语,则返回以下json

    [{
    "id": 1,
    "description": "desc",
    "offer_types_id": 1,
    "languages_id": 2,
    "title": "'u062d'u0632'u0645"
}, {
    "id": 2,
    "description": "desc",
    "offer_types_id": 2,
    "languages_id": 2,
    "title": "'u0641'u0646'u0627'u062f'u0642"
}]

如何在 Laravel 5.2 中对此arabic值进行编码?

尝试这样做:

$data = DB::table('offer_types')
                    ->join('offer_type_details', 'offer_type_details.offer_types_id', '=', 'offer_types.id')
                    ->where('offer_type_details.languages_id', $request->language_id ? $request->language_id : 1)
                    ->get();
return Response::json($data, 200, [], JSON_UNESCAPED_UNICODE);

return response()->json($data, 200, [], JSON_UNESCAPED_UNICODE);

使用JSON_UNESCAPED_UNICODE标志进行 JSON 编码。

json_encode($multibyte_string, JSON_UNESCAPED_UNICODE);

它按字面意思对多字节 Unicode 字符进行编码。所以 Unicode 字符不会像 'uXXXX 那样被转义。