Détecte si la chaîne est encodée en UTF8 ou non
@param string
@returns {Boolean}
@link https://github.com/wayfind/is-utf8
341737
function(string) {
            
            var i = 0;
            while(i < string.length)
            {
                if(     (// ASCII
                string[i] == 0x09 ||
                    string[i] == 0x0A ||
                    string[i] == 0x0D ||
                    (0x20 <= string[i] && string[i] <= 0x7E)
                    )
                    ) {
                    i += 1;
                    continue;
                }
                
                if(     (// non-overlong 2-byte
                (0xC2 <= string[i] && string[i] <= 0xDF) &&
                    (0x80 <= string[i+1] && string[i+1] <= 0xBF)
                    )
                    ) {
                    i += 2;
                    continue;
                }
                
                if(     (// excluding overlongs
                string[i] == 0xE0 &&
                    (0xA0 <= string[i + 1] && string[i + 1] <= 0xBF) &&
                    (0x80 <= string[i + 2] && string[i + 2] <= 0xBF)
                    ) ||
                    (// straight 3-byte
                ((0xE1 <= string[i] && string[i] <= 0xEC) ||
                    string[i] == 0xEE ||
                    string[i] == 0xEF) &&
                    (0x80 <= string[i + 1] && string[i+1] <= 0xBF) &&
                    (0x80 <= string[i+2] && string[i+2] <= 0xBF)
                    ) ||
                    (// excluding surrogates
                string[i] == 0xED &&
                    (0x80 <= string[i+1] && string[i+1] <= 0x9F) &&
                    (0x80 <= string[i+2] && string[i+2] <= 0xBF)
                    )
                    ) {
                    i += 3;
                    continue;
                }
                
                if(     (// planes 1-3
                string[i] == 0xF0 &&
                    (0x90 <= string[i + 1] && string[i + 1] <= 0xBF) &&
                    (0x80 <= string[i + 2] && string[i + 2] <= 0xBF) &&
                    (0x80 <= string[i + 3] && string[i + 3] <= 0xBF)
                    ) ||
                    (// planes 4-15
                (0xF1 <= string[i] && string[i] <= 0xF3) &&
                    (0x80 <= string[i + 1] && string[i + 1] <= 0xBF) &&
                    (0x80 <= string[i + 2] && string[i + 2] <= 0xBF) &&
                    (0x80 <= string[i + 3] && string[i + 3] <= 0xBF)
                    ) ||
                    (// plane 16
                string[i] == 0xF4 &&
                    (0x80 <= string[i + 1] && string[i + 1] <= 0x8F) &&
                    (0x80 <= string[i + 2] && string[i + 2] <= 0xBF) &&
                    (0x80 <= string[i + 3] && string[i + 3] <= 0xBF)
                    )
                    ) {
                    i += 4;
                    continue;
                }
                
                return false;
            }
            
            return true;
        }
737717
static public function isUTF8($string)
    {
        for ($idx = 0, $strlen = strlen($string); $idx < $strlen; $idx++) {
            $byte = ord($string[$idx]);

            if ($byte & 0x80) {
                if (($byte & 0xE0) == 0xC0) {
                    $bytes_remaining = 1;
                } else if (($byte & 0xF0) == 0xE0) {
                    $bytes_remaining = 2;
                } else if (($byte & 0xF8) == 0xF0) {
                    $bytes_remaining = 3;
                } else {
                    return false;
                }

                if ($idx + $bytes_remaining >= $strlen) {
                    return false;
                }

                while ($bytes_remaining--) {
                    if ((ord($string[++$idx]) & 0xC0) != 0x80) {
                        return false;
                    }
                }
            }
        }

        return true;
    }
626194
private function isUtf8($string)
    {
        $encoding = mb_detect_encoding($string);
        if ($encoding == 'UTF-8') {
            return true;
        }
        
        $regex = array(
            '[\xC2-\xDF][\x80-\xBF]',
            '\xE0[\xA0-\xBF][\x80-\xBF]',
            '[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}',
            '\xED[\x80-\x9F][\x80-\xBF]',
            '\xF0[\x90-\xBF][\x80-\xBF]{2}',
            '[\xF1-\xF3][\x80-\xBF]{3}',
            '\xF4[\x80-\x8F][\x80-\xBF]{2}');
        
        $count = ceil(strlen($string) / 5000);
        for ($i = 0; $i < $count; $i++) {
            if (preg_match('%(?:'. implode('|', $regex).')+%xs', substr($string, ($i * 5000), 5000))) {
                return false;
            }
        }
        
        return true;
    }
528307
function detectEncoding(label, input_stream) {
      if (input_stream.match([0xFF, 0xFE])) {
        input_stream.offset(2);
        return 'utf-16';
      }
      if (input_stream.match([0xFE, 0xFF])) {
        input_stream.offset(2);
        return 'utf-16be';
      }
      if (input_stream.match([0xEF, 0xBB, 0xBF])) {
        input_stream.offset(3);
        return 'utf-8';
      }
      return label;
    }
640154
public static function check_encoding($data)
    {
        if (!is_array($data)) {
            return;
        }

        foreach ($data as $key => $value) {
            if (!is_array($value)) {
                // check if the multibyte library function is installed and use it.
                if (function_exists('mb_detect_encoding')) {
                    // check with mb library
                    if (is_string($key) && mb_detect_encoding($key, 'UTF-8', true) === false) {
                        throw new ClientException('Only UTF-8 encoded keys allowed. Wrong encoding in key string: ' . $key);
                    }
                    if (is_string($value) && mb_detect_encoding($value, 'UTF-8', true) === false) {
                        throw new ClientException('Only UTF-8 encoded values allowed. Wrong encoding in value string: ' . $value);
                    }
                } else {
                    // fallback to preg_match checking
                    if (is_string($key) && self::detect_utf($key) === false) {
                        throw new ClientException('Only UTF-8 encoded keys allowed. Wrong encoding in key string: ' . $key);
                    }
                    if (is_string($value) && self::detect_utf($value) === false) {
                        throw new ClientException('Only UTF-8 encoded values allowed. Wrong encoding in value string: ' . $value);
                    }
                }
            } else {
                self::check_encoding($value);
            }
        }
    }
685497
def convert_to_utf8(string):
    '''  '''
    if (isinstance(string, unicode)):
        return string.encode('utf-8')
    try:
        u = unicode(string, 'utf-8')
    except TypeError:
        return str(string)
    utf8 = u.encode('utf-8')
    return utf8
105102
public static function seemsUtf8($string)
    {
        $length = strlen($string);
        for ($i=0; $i < $length; $i++) {
            $c = ord($string[$i]);
            if ($c < 0x80) $n = 0; # 0bbbbbbb
            elseif (($c & 0xE0) == 0xC0) $n=1; # 110bbbbb
            elseif (($c & 0xF0) == 0xE0) $n=2; # 1110bbbb
            elseif (($c & 0xF8) == 0xF0) $n=3; # 11110bbb
            elseif (($c & 0xFC) == 0xF8) $n=4; # 111110bb
            elseif (($c & 0xFE) == 0xFC) $n=5; # 1111110b
            else return false; # Does not match any model
            for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ?
                if ((++$i == $length) || ((ord($string[$i]) & 0xC0) != 0x80))
                    return false;
            }
        }

        return true;
    }
1294666
static function realstring($str,$encode='utf8')
    {
        if($encode=='utf8') $n = preg_match('/^[\x{4e00}-\x{9fa5}|a-z|0-9|A-Z]+$/u',$str,$match);
        else $n = preg_match("/^[".chr(0xa1)."-".chr(0xff)."|a-z|0-9|A-Z]+$/",$str,$match);
        if($n===0) return false;
        else return $match[0];
    }
33874
public static function strlenForUtf8($string)
	{
		$l = strlen($string);
		$p = $c = 0;
		while ($p < $l)
		{
			$a = $string[$p];
			if ($a < "\xC0")
				$p++;
			elseif ($a < "\xE0")
				$p += 2;
			elseif ($a < "\xF0")
				$p += 3;
			elseif ($a < "\xF8")
				$p += 4;
			elseif ($a < "\xFC")
				$p += 5;
			else
				$p += 6;
			$c++;
		}
		return $c;
	}
508233
function utf8CheckIncomplete(self, buf, i) {
  var j = buf.length - 1;
  if (j < i) return 0;
  var nb = utf8CheckByte(buf[j]);
  if (nb >= 0) {
    if (nb > 0) self.lastNeed = nb - 1;
    return nb;
  }
  if (--j < i) return 0;
  nb = utf8CheckByte(buf[j]);
  if (nb >= 0) {
    if (nb > 0) self.lastNeed = nb - 2;
    return nb;
  }
  if (--j < i) return 0;
  nb = utf8CheckByte(buf[j]);
  if (nb >= 0) {
    if (nb > 0) {
      if (nb === 2) nb = 0;else self.lastNeed = nb - 3;
    }
    return nb;
  }
  return 0;
}
670906
protected boolean canBeUTF8(
        String  str)
    {
        for (int i = str.length() - 1; i >= 0; i--)
        {
            if (str.charAt(i) > 0x00ff)
            {
                return false;
            }
        }

        return true;
    }
744964
public function convertToUTF8($string){
			if (is_array($string)) {
				foreach ($string as $k => $v) {
					$string[$k] = $this->convertToUTF8($v);
				}
			} else if (is_string ($string)) {
				return utf8_encode($string);
			}
			return $string;
        }
374891
function utf8_to_entities($str) {
		$strLen = strlen($str);
		$outStr = '';
		$buf = '';
		for ($a = 0; $a < $strLen; $a++) { // Traverse each char in UTF-8 string.
			$chr = substr($str, $a, 1);
			$ord = ord($chr);
			if ($ord > 127) { // This means multibyte! (first byte!)
				if ($ord & 64) { // Since the first byte must have the 7th bit set we check that. Otherwise we might be in the middle of a byte sequence.
					$buf = $chr; // Add first byte
					for ($b = 0; $b < 8; $b++) { // for each byte in multibyte string...
						$ord = $ord << 1; // Shift it left and ...
						if ($ord & 128) { // ... and with 8th bit - if that is set, then there are still bytes in sequence.
							$a++; // Increase pointer...
							$buf .= substr($str, $a, 1); // ... and add the next char.
						} else {
							break;
						}
					}

					$outStr .= '&#' . $this->utf8CharToUnumber($buf, 1) . ';';
				} else {
					$outStr .= chr($this->noCharByteVal);
				} // No char exists (MIDDLE of MB sequence!)
			} else {
				$outStr .= $chr;
			} // ... otherwise it's just ASCII 0-127 and one byte. Transparent
		}

		return $outStr;
	}
813372
function utf16to8(e) {
    var t, n, r, i;

    t = '';
    r = e.length;

    for (n = 0; n < r; n++) {
        i = e.charCodeAt(n);

        if (i >= 1 && i <= 127) {
            t += e.charAt(n);
        } else if (i > 2047) {
            t += String.fromCharCode(224 | i >> 12 & 15);
            t += String.fromCharCode(128 | i >> 6 & 63);
            t += String.fromCharCode(128 | i >> 0 & 63);
        } else {
            t += String.fromCharCode(192 | i >> 6 & 31);
            t += String.fromCharCode(128 | i >> 0 & 63);
        }
    }

    return t;
}
569815
function(str) {
    if(typeof(str)=='undefined') return undefined;
    if(typeof(str)=='undefined') return undefined;

    var surrogate_1st = 0;
    var unicode_codes = [];
    for (var i = 0; i < str.length; ++i) {
	var utf16_code = str.charCodeAt(i);
	if (surrogate_1st != 0) {
	    if (utf16_code >= 0xdc00 && utf16_code <= 0xdfff) {
		var surrogate_2nd = utf16_code;
		var unicode_code = (surrogate_1st - 0xd800) * (1 << 10) + (1 << 16) +
                    (surrogate_2nd - 0xdc00);
		unicode_codes.push(unicode_code);
	    }
	    surrogate_1st = 0;
	}
	else if (utf16_code >= 0xd800 && utf16_code <= 0xdbff) {
	    surrogate_1st = utf16_code;
	}
	else {
	    unicode_codes.push(utf16_code);
	}
    }

    var utf8_bytes = "";
    var i, unicode_code;
    for(i=0; i<unicode_codes.length; ++i) {
	unicode_code = unicode_codes[i];
	if (unicode_code < 0x80) {  // 1-byte
	    utf8_bytes += String.fromCharCode(unicode_code);
	}
	else if (unicode_code < (1 << 11)) {  // 2-byte
	    utf8_bytes += String.fromCharCode((unicode_code >>> 6) | 0xC0);
	    utf8_bytes += String.fromCharCode((unicode_code & 0x3F) | 0x80);
	}
	else if (unicode_code < (1 << 16)) {  // 3-byte
	    utf8_bytes += String.fromCharCode((unicode_code >>> 12) | 0xE0);
	    utf8_bytes += String.fromCharCode(((unicode_code >> 6) & 0x3f) | 0x80);
	    utf8_bytes += String.fromCharCode((unicode_code & 0x3F) | 0x80);
	}
	else if (unicode_code < (1 << 21)) {  // 4-byte
	    utf8_bytes += String.fromCharCode((unicode_code >>> 18) | 0xF0);
	    utf8_bytes += String.fromCharCode(((unicode_code >> 12) & 0x3F) | 0x80);
	    utf8_bytes += String.fromCharCode(((unicode_code >> 6) & 0x3F) | 0x80);
	    utf8_bytes += String.fromCharCode((unicode_code & 0x3F) | 0x80);
	}
    }
    return utf8_bytes;
}
749147
function __decode_utf8(buffer) {

        var text = "";

        var bytes = new Uint8Array(buffer);
        for (var i=0; i<bytes.length; i++) {

            // Get current byte
            var value = bytes[i];

            // Start new codepoint if nothing yet read
            if (bytes_remaining === 0) {

                // 1 byte (0xxxxxxx)
                if ((value | 0x7F) === 0x7F)
                    text += String.fromCharCode(value);

                // 2 byte (110xxxxx)
                else if ((value | 0x1F) === 0xDF) {
                    codepoint = value & 0x1F;
                    bytes_remaining = 1;
                }

                // 3 byte (1110xxxx)
                else if ((value | 0x0F )=== 0xEF) {
                    codepoint = value & 0x0F;
                    bytes_remaining = 2;
                }

                // 4 byte (11110xxx)
                else if ((value | 0x07) === 0xF7) {
                    codepoint = value & 0x07;
                    bytes_remaining = 3;
                }

                // Invalid byte
                else
                    text += "\uFFFD";

            }

            // Continue existing codepoint (10xxxxxx)
            else if ((value | 0x3F) === 0xBF) {

                codepoint = (codepoint << 6) | (value & 0x3F);
                bytes_remaining--;

                // Write codepoint if finished
                if (bytes_remaining === 0)
                    text += String.fromCharCode(codepoint);

            }

            // Invalid byte
            else {
                bytes_remaining = 0;
                text += "\uFFFD";
            }

        }

        return text;

    }
654323
public static function utf8_to_entities($str, $dec=false, $nonnum=false) {
        static $callback = null ;

        if ($nonnum) {
            $str = self::entities_to_utf8($str, true);
        }

        // Avoid some notices from Typo3 code
        $oldlevel = error_reporting(E_PARSE);
        $result = self::typo3()->utf8_to_entities((string)$str);
        error_reporting($oldlevel);

        if ($dec) {
            if (!$callback) {
                $callback = function($matches) {
                    return '&#' . hexdec($matches[1]) . ';';
                };
            }
            $result = preg_replace_callback('/&#x([0-9a-f]+);/i', $callback, $result);
        }

        return $result;
    }