Posted By

iusik on 10/08/10


Tagged

jsonencode


Versions (?)

json_encode for Cyrillic Language


 / Published in: PHP
 

  1. function json_safe_encode($var)
  2. {
  3. return json_encode(json_fix_cyr($var));
  4. }
  5.  
  6. function json_fix_cyr($var)
  7. {
  8. if (is_array($var)) {
  9. $new = array();
  10. foreach ($var as $k => $v) {
  11. $new[json_fix_cyr($k)] = json_fix_cyr($v);
  12. }
  13. $var = $new;
  14. } elseif (is_object($var)) {
  15. $vars = get_object_vars($var);
  16. foreach ($vars as $m => $v) {
  17. $var->$m = json_fix_cyr($v);
  18. }
  19. } elseif (is_string($var)) {
  20. $var = iconv('cp1251', 'utf-8', $var);
  21. }
  22. return $var;
  23. }
  24.  
  25. //Some array with Cyrillic letters $cyrillic_word.
  26.  
  27.  
  28. json_safe_encode($cyrillic_word);

Report this snippet  

You need to login to post a comment.