-2

json_encode not working with urldecode. Please give some solution

<?php
$decode_str = array();
$str = "%ED%A0%BD%ED%BA%97%D9%BE%D8%B1%D8%A7%DB%8C%D8%AF+89%D8%A8%D8%AF%D9%88%D9%86+%D8%B1%D9%86%DA%AF.+%D8%AA%DA%A9+%D8%B3%D9%88%D8%B2%ED%A0%BD%ED%BA%97";

$decode_str['output'] = urldecode($str);
echo json_encode($decode_str);
?>

2 Answers2

1

If you add echo json_last_error_msg(); to the end of it, you will see that there is a json error.

Malformed UTF-8 characters, possibly incorrectly encoded

The string you are trying to encode has malformed UTF-8 Characters. You will need to fix the characted encoding before you can encode it into json.

James Wallen-Jones
  • 1,008
  • 8
  • 10
1

Use utf8_decode() as well.

$decode_str = array();

$str = "%ED%A0%BD%ED%BA%97%D9%BE%D8%B1%D8%A7%DB%8C%D8%AF+89%D8%A8%D8%AF%D9%88%D9%86+%D8%B1%D9%86%DA%AF.+%D8%AA%DA%A9+%D8%B3%D9%88%D8%B2%ED%A0%BD%ED%BA%97";

$decode_str['output'] = utf8_decode(urldecode($str));

echo json_encode($decode_str);
Ahsan
  • 1,084
  • 2
  • 15
  • 28