Skip to content
Home Sinan Eldem
Software

Fixing Turkish Character Issues with JSON Encode / Decode

Sinan Eldem

• 5 min read

Fixing Turkish Character Issues with JSON Encode / Decode

JSON has certainly been very useful since it came into our lives, but when Turkish characters are involved, problems are inevitable.

In this article, we'll look at a very simple way to get around this problem. Note, however, that the solution I suggest works from PHP 5.4.0 onwards; if you're on an earlier version, you'll need to upgrade PHP.

The sample text I'll use is an array:

$array= array(array('turkish' => 'üğışçöÜĞİŞÇÖ'), array('chinese' => '我爱你'), array('arabic' => 'أنا أحبك'));

As you can see, alongside characters specific to Turkish, I also tried to write “I love you” in Chinese and Arabic; if there are any mistakes in the Chinese or Arabic, it's because Google Translate doesn't know how to love.

Let's create a file named json.php and add the following to it:

<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
<?php
function json_encode_tr($string)
{
    return json_encode($string, JSON_UNESCAPED_UNICODE);
}

$array= array(array('turkish' =--> 'üğışçöÜĞİŞÇÖ'), array('chinese' => '我爱你'), array('arabic' => 'أنا أحبك'));
echo 'Örnek Dizi: ';
var_dump($array);
echo '<br>';

// Örnek Dizi: array(3) { [0]=> array(1) { ["turkish"]=> string(24) "üğışçöÜĞİŞÇÖ" } [1]=> array(1) { ["chinese"]=> string(9) "我爱你" } [2]=> array(1) { ["arabic"]=> string(15) "أنا أحبك" } } 

$encoded = json_encode($array);
echo "Standart Encoded Json: ";
var_dump($encoded);;
echo '<br>';

// Standart Encoded Json: string(178) "[{"turkish":"\u00fc\u011f\u0131\u015f\u00e7\u00f6\u00dc\u011e\u0130\u015e\u00c7\u00d6"},{"chinese":"\u6211\u7231\u4f60"},{"arabic":"\u0623\u0646\u0627 \u0623\u062d\u0628\u0643"}]" 

$encodedTr = json_encode_tr($array);
echo "Türkçe Encoded Json: ";
var_dump($encodedTr);
echo '<br>';

// Türkçe Encoded Json: string(93) "[{"turkish":"üğışçöÜĞİŞÇÖ"},{"chinese":"我爱你"},{"arabic":"أنا أحبك"}]" 

$decoded = json_decode($encoded);
echo "Decoded Json: ";
var_dump($decoded);
echo '<br>';

// Decoded Json: array(3) { [0]=> object(stdClass)#1 (1) { ["turkish"]=> string(24) "üğışçöÜĞİŞÇÖ" } [1]=> object(stdClass)#2 (1) { ["chinese"]=> string(9) "我爱你" } [2]=> object(stdClass)#3 (1) { ["arabic"]=> string(15) "أنا أحبك" } } 

$decodedTr = json_decode($encodedTr);
echo "Türkçe Decoded Json: ";
var_dump($encodedTr);
echo '<br>';

// Türkçe Decoded Json: string(93) "[{"turkish":"üğışçöÜĞİŞÇÖ"},{"chinese":"我爱你"},{"arabic":"أنا أحبك"}]" 

Our page is now set to display UTF-8; let's save it as UTF-8 without BOM. We've added the function we need, our examples and the screen output.

When you look at the examples, you'll see that JSON_UNESCAPED_UNICODE is what comes to the rescue here:

json_encode($string, JSON_UNESCAPED_UNICODE);

Feel free to use the comment section below for any questions.

Sinan Eldem

Custom software and AI solutions with Laravel

Related posts

Software
6 min read

Laravel Eloquent Relationships Quick Reference Guide

This quick reference is a practical guide that explains the most common relationship types with simple examples. Covering hasOne, hasMany, belongsTo, belongsToMany and polymorphic relationships along with eager loading and performance tips, it gives developers a fast and accurate reference.

Read more