Skip to content

Instantly share code, notes, and snippets.

@jstxx
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jstxx/11906e19cddee96cb074 to your computer and use it in GitHub Desktop.
Save jstxx/11906e19cddee96cb074 to your computer and use it in GitHub Desktop.
<?php
function unicode_decode ( $string )
{
function decode_unicode ( $c )
{
return ( ( $c = ( isset ( $c[3] ) ? $c[3]
:
( isset ( $c[2] ) ? hexdec ( $c[2] )
:
hexdec ( $c[1] ) ) ) ) < 0x80 ? chr ( $c )
:
( $c < 0x800 ? chr ( 0xc0 | ( $c >> 6 ) ) . chr ( 0x80 | ( $c & 0x3f ) )
:
( $c < 0x10000 ? chr ( 0xe0 | ( $c >> 12 ) ) . chr ( 0x80 | ( ( $c >> 6 ) & 0x3f ) ) . chr ( 0x80 | ( $c & 0x3f ) )
:
( $c < 0x200000 ? chr ( 0xf0 | ( $c >> 18 ) ) . chr ( 0x80 | ( ( $c >> 12 ) & 0x3f ) ) . chr ( 0x80 | ( ( $c >> 6 ) & 0x3f ) ) . chr ( 0x80 | ( $c & 0x3f ) ) : '' ) ) )
);
}
return preg_replace_callback ( '~(?:\\\\\\u|%u)([0-9a-f]{4})|&#x0*([0-9a-f]+);|&#0*([0-9]+);~i', 'decode_unicode', $string );
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment