Malformed Utf-8 Characters, Possibly Incorrectly Encoded' in Laravel

Laravel malformed character UTF-8

You are using strings that are coming from the filesystem filenames. Such strings are typically not in UTF-8 and use the ISO-8859-1 (usually). Coincidentally this is the required input encoding which utf8_encode requires to work.

$drives = ['M1', 'M2', 'M3', 'M4'];
$movies = [];

foreach ($drives as $drive) {
$disk = Storage::disk($drive);
foreach ($disk -> directories() as $movie) {
$movies[] = utf8_encode($movie);
}
}

return response()->json($movies, 200, ['Content-type'=> 'application/json; charset=utf-8'], JSON_UNESCAPED_UNICODE);

However, if you do wish to convert to UTF-8 from another (known) encoding you need to use mb_convert_encoding. Overall be aware that setting the HTTP response encoding to UTF-8 will not automatically convert any character encodings. You have to do that manually.

Laravel malformed UTF-8 characters, possibly incorrectly encoded using image intervention

The problem was this line, I was saving the actual image object in the db.

 $product->image = \Image::make($request->image)->save(public_path('img/').$fileName);

I changed it into

$imageData = $request->image;
$fileName = time().'.'. explode('/', explode(':', substr($imageData, 0, strpos($imageData, ';'))) [1])[1];

\Image::make($request->image)->save(public_path('img/').$fileName);

$product->image = $fileName;
$product->save();;

possibly incorrectly encoded,Malformed UTF-8 characters

In my laravel query i am use a following code so it will give a this type of error...

Malformed UTF-8 characters, possibly incorrectly encoded

$BvoData = DB::table('test1')->select('test1.*')
->where("test1.Id", "".$id."")
->first();

$BvoData->temp1 = DB::table('temp1')->where('tmpdata', $BvoData->tmpdata)->get();

$BvoData->temp2 = DB::table('temp2')->where('Id', $id)->get();

return response()->json($BvoData);

but i will solve this error by doing following code...

$BvoData = DB::table('test1')->select('test1.*')
->where("test1.Id", "".$id."")
->first();

$BvoData = (array) $BvoData;

$BvoData->temp1 = DB::table('temp1')->where('tmpdata', $BvoData->tmpdata)->get();

$BvoData["temp1"] = json_decode(json_encode($BvoData["temp1"]), True);

$BvoData->temp2 = DB::table('temp2')->where('Id', $id)->get();

$BvoData["temp2"] = json_decode(json_encode($BvoData["temp2"]), True);

return response()->json($BvoData);

by using json_decode and json_encode i solve my problem...



Related Topics



Leave a reply



Submit