0

I have a string test éàå. when I am performing a json_encode, the special character is being converted to test u00e9u00e0u00e5.

Now when performing a json_decode, I am not getting back the original characters but instead it is remaining as test u00e9u00e0u00e5

Can someone help me to have the special character back.

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
pris
  • 50
  • 1
  • 10

1 Answers1

0

json_encode and json_decode only work with UTF-8 encoded strings.

To ensure it displays correctly in your webpage, use utf-8 charset.

<!DOCTYPE html>
<html lang="en">
<head>    
<meta charset="utf-8">    
</head>
<body>
<?php
    $string = 'test éàå';
    $json = json_encode($string);
    echo json_decode($json);
?>
</body>
</html>
Tan Hong Tat
  • 6,671
  • 2
  • 27
  • 25
  • Thanks...this works perfect. But what if I am inserting an array of data in a database and trying to get it back, e.g json_encode($_POST); And I try to do this json_decode($result) It stills give me the encoded special character. – pris Feb 05 '14 at 06:21
  • Ensure the html form is in utf-8, and you should have no problem inserting and retriving the data. – Tan Hong Tat Feb 05 '14 at 06:27
  • Sorry for the stupid ques. How can I ensure this. And also I am using a index.php and then performing an insert into dbase – pris Feb 05 '14 at 08:03