Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. <?php
  2. /**
  3. * Using json_encode() with some character sets can result in false being returned.
  4. * These functions allow creation of JSON strings from any value/object that may need
  5. * UTF-8 encoding.
  6. */
  7.  
  8.  
  9. /**
  10. * Given the output from serialize(), this will encode or decode UTF-8 characters
  11. * and adjust length markers as necessary.
  12. *
  13. * @param string $input The input string
  14. * @param boolean $encode Are we encoding (or decoding) the string
  15. * @return string
  16. */
  17. function utf8Adjust($input, $encode=TRUE) {
  18. if (preg_match('//u', $input) === 1) {
  19. // Already using UTF-8, no encoding to do
  20.  
  21. if ($encode) return $input;
  22. } else {
  23. // Not UTF-8, no decoding to do
  24.  
  25. if (!$encode) return $input;
  26. }
  27.  
  28. $output = '';
  29.  
  30. // Copy input string characters until we get to 's' - a string, or 'O' - a class name
  31. for ($i=0; $i<strlen($input); ++$i) {
  32. $output .= $input{$i};
  33.  
  34. if ($input{$i} === 's' || $input{$i} === 'O') {
  35. ++$i; // ':'
  36.  
  37. // After 's:' or 'O:' is the length
  38. $length = '';
  39.  
  40. while ($input{++$i} !== ':') {
  41. $length .= $input{$i};
  42. }
  43.  
  44. $length = (int) $length;
  45.  
  46. $i += 2; // ':"'
  47.  
  48. // Now we can get the string itself
  49. $string = substr($input, $i, $length);
  50.  
  51. // We can use this function to encode and decode
  52. $string = ($encode) ? utf8_encode($string) : utf8_decode($string);
  53.  
  54. // Join everything back together
  55. $output .= ':' . strlen($string) . ':"' . $string . '"';
  56.  
  57. $i += $length;
  58. }
  59. }
  60.  
  61. return $output;
  62. }
  63.  
  64. /**
  65. * UTF-8 encode an input value
  66. *
  67. * @param mixed $input
  68. * @return mixed
  69. */
  70. function utf8Encode($input) {
  71. return unserialize(utf8Adjust(serialize($input), TRUE));
  72. }
  73.  
  74. /**
  75. * UTF-8 decode an input value
  76. *
  77. * @param mixed $input
  78. * @return mixed
  79. */
  80. function utf8Decode($input) {
  81. return unserialize(utf8Adjust(serialize($input), FALSE));
  82. }
  83.  
  84. /**
  85. * JSON encode - handling UTF-8 encoding
  86. *
  87. * @param mixed $input
  88. * @return string
  89. */
  90. function jsonEncode($input) {
  91. return json_encode(utf8Encode($input));
  92. }
  93.  
  94. /**
  95. * JSON decode - handling UTF-8 encoding
  96. *
  97. * @param string $input
  98. * @param boolean $assoc [optional] When TRUE, returned objects will be
  99. * converted into associative arrays.
  100. * @param integer $depth [optional] User specified recursion depth.
  101. * @param integer $options [optional] Bitmask of JSON decode options.
  102. * Currently only JSON_BIGINT_AS_STRING is supported (default is to cast
  103. * large integers as floats)
  104. * @return mixed
  105. */
  106. function jsonDecode($input, $assoc=FALSE, $depth=512, $options=0) {
  107. return utf8Decode(json_decode($input, $assoc, $depth, $options));
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement