Skip to content

Instantly share code, notes, and snippets.

@bordoni
Created March 5, 2012 08:43
Show Gist options
  • Save bordoni/1977499 to your computer and use it in GitHub Desktop.
Save bordoni/1977499 to your computer and use it in GitHub Desktop.
Multi Byte JSON PHP
<?php
function array_map_recursive( $callback, $mixed ) {
if( is_array( $mixed ) || is_object( $mixed ) ) {
$return = is_array( $mixed ) ? array() : new stdClass;
foreach ($mixed as $key => $value){
if( is_array( $mixed ) )
$return[call_user_func( $callback, $key )] = array_map_recursive( $callback, $value );
else
$return->{call_user_func( $callback, $key )} = array_map_recursive( $callback, $value );
}
return $return;
} else {
return call_user_func( $callback, $mixed );
}
}
function mb_json_encode( $mixed ) {
$apply = function ( $string ) {
return str_replace( array( "\"", "'" ), array( "\u0022", "\u0027" ), utf8_encode( $string ) );
};
return json_encode( array_map_recursive( $apply, $mixed ) );
}
function mb_json_decode( $string ) {
$apply = function ( $string ) {
return str_replace( array( "\u0022", "\u0027" ), array( "\"", "'" ), utf8_decode( $string ) );
};
$on = json_decode( str_replace( "\\\"", "\"", $string ), true );
return array_map_recursive( $apply, $on );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment