You are here

function Services_JSON::utf162utf8 in Video 7

convert a string from one UTF-16 char to one UTF-8 char

Normally should be handled by mb_convert_encoding, but provides a slower PHP-only method for installations that lack the multibye string extension.

@access private

Parameters

string $utf16 UTF-16 character:

Return value

string UTF-8 character

1 call to Services_JSON::utf162utf8()
Services_JSON::decode in modules/video_zencoder/includes/lib/JSON.php
decodes a JSON string into appropriate variable

File

modules/video_zencoder/includes/lib/JSON.php, line 148

Class

Services_JSON
Converts to and from JSON format.

Code

function utf162utf8($utf16) {

  // oh please oh please oh please oh please oh please
  if (function_exists('mb_convert_encoding')) {
    return mb_convert_encoding($utf16, 'UTF-8', 'UTF-16');
  }
  $bytes = ord($utf16[0]) << 8 | ord($utf16[1]);
  switch (true) {
    case (0x7f & $bytes) == $bytes:

      // this case should never be reached, because we are in ASCII range
      // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
      return chr(0x7f & $bytes);
    case (0x7ff & $bytes) == $bytes:

      // return a 2-byte UTF-8 character
      // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
      return chr(0xc0 | $bytes >> 6 & 0x1f) . chr(0x80 | $bytes & 0x3f);
    case (0xffff & $bytes) == $bytes:

      // return a 3-byte UTF-8 character
      // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
      return chr(0xe0 | $bytes >> 12 & 0xf) . chr(0x80 | $bytes >> 6 & 0x3f) . chr(0x80 | $bytes & 0x3f);
  }

  // ignoring UTF-32 for now, sorry
  return '';
}