Encodes an array of data to be UTF8 over using html entities

@var array
505670
public static function utf8_json_encode($arr, $options = 0, $depth = 512) {
		// NOTE(Jake): Might be able to get more speed out of this (if need be) by making it just json_encode
		//			   and if it fails with JSON_ERROR_UTF8, then do the recursive walk
		$utf8_arr = $arr;
		array_walk_recursive($utf8_arr, array(__CLASS__, '_utf8_json_encode_recursive'));
		$result = json_encode($utf8_arr, $options, $depth);
		return $result;
	}
1021275
function encodeTags(tags) {
  var bufLength = 0;
  var tagLengths = []
  
  for (var i = 0; i < tags.length; i++) {
    var tagLength = getTagLength(tags[i]);
    tagLengths.push(tagLength);
    bufLength += tagLength;
  }
  
  var buf = new Buffer(bufLength);
  var slicedBuf = buf;
  
  for (var i = 0; i < tags.length; i++) {
    encodeNumber(slicedBuf, tags[i], tagLengths[i]);
    slicedBuf = slicedBuf.slice(tagLengths[i]);
  }
  
  return buf;
}
179079
function encodeEntities(value) {
  return value.
    replace(/&/g, '&amp;').
    replace(NON_ALPHANUMERIC_REGEXP, function(value){
      return '&#' + value.charCodeAt(0) + ';';
    }).
    replace(/</g, '&lt;').
    replace(/>/g, '&gt;');
}
292090
public static function utf8Encode($data) {
		if (is_string($data)) {
			return utf8_encode($data);

		} else if (is_array($data)) {
			foreach ($data as $key => $value) {
				$data[utf8_encode($key)] = self::utf8Encode($value);
			}

		} else if (is_object($data)) {
			foreach ($data as $key => $value) {
				$data->{$key} = self::utf8Encode($value);
			}
		}

		return $data;
	}
1053937
function(data) {

    var _mode = QRMode.MODE_8BIT_BYTE;
    var _data = data;
    var _parsedData = [];

    var _this = {};


    // Added to support UTF-8 Characters
    for (var i = 0, l = _data.length; i < l; i++) {
	var byteArray = [];
	var code = _data.charCodeAt(i);

	if (code > 0x10000) {
	    byteArray[0] = 0xF0 | ((code & 0x1C0000) >>> 18);
	    byteArray[1] = 0x80 | ((code & 0x3F000) >>> 12);
	    byteArray[2] = 0x80 | ((code & 0xFC0) >>> 6);
	    byteArray[3] = 0x80 | (code & 0x3F);
	} else if (code > 0x800) {
	    byteArray[0] = 0xE0 | ((code & 0xF000) >>> 12);
	    byteArray[1] = 0x80 | ((code & 0xFC0) >>> 6);
	    byteArray[2] = 0x80 | (code & 0x3F);
	} else if (code > 0x80) {
	    byteArray[0] = 0xC0 | ((code & 0x7C0) >>> 6);
	    byteArray[1] = 0x80 | (code & 0x3F);
	} else {
	    byteArray[0] = code;
	}

        // Fix Unicode corruption bug
        _parsedData.push(byteArray);
    }

    _parsedData = Array.prototype.concat.apply([], _parsedData);

    if (_parsedData.length != _data.length) {
	_parsedData.unshift(191);
	_parsedData.unshift(187);
	_parsedData.unshift(239);
    }

    var _bytes = _parsedData;

    _this.getMode = function() {
	return _mode;
    };

    _this.getLength = function(buffer) {
	return _bytes.length;
    };

    _this.write = function(buffer) {
	for (var i = 0; i < _bytes.length; i += 1) {
	    buffer.put(_bytes[i], 8);
	}
    };

    return _this;
}
10231
static function convertUnicodeToString( $unicodeValues )
    {
        if ( !is_array( $unicodeValues ) )
            return false;
        $text = '';
        foreach ( $unicodeValues as $unicodeValue )
        {
            $utf8Char = eZUTF8Codec::toUTF8( $unicodeValue );
            $text .= $utf8Char;
        }
        return $text;
    }
777465
public static function str_safe_html(&$array)
    {
        if (is_array($array)) {
            foreach ($array as $key => $value) {
                if (!is_array($value)) {
                    //&,",',> ,< 转为html实体 &amp;,&quot;&#039;,&gt;,&lt;
                    $array[$key] = htmlspecialchars($value,ENT_QUOTES);
                } else {
                    self::str_safe_html($array[$key]);
                }
            }
        } else {
            $array = htmlspecialchars($array,ENT_QUOTES);
        }
    }
538636
static function json_encode($arr) {
        if (count($arr) == 0) return "[]";
        $parts = array ();
        $is_list = false;
        //Find out if the given array is a numerical array
        $keys = array_keys ( $arr );
        $max_length = count ( $arr ) - 1;
        if (($keys [0] === 0) && ($keys [$max_length] === $max_length )) { //See if the first key is 0 and last key is length - 1
            $is_list = true;
            for($i = 0; $i < count ( $keys ); $i ++) { //See if each key correspondes to its position
                if ($i != $keys [$i]) { //A key fails at position check.
                    $is_list = false; //It is an associative array.
                    break;
                }
            }
        }
        foreach ( $arr as $key => $value ) {
            if (is_array ( $value )) { //Custom handling for arrays
                if ($is_list)
                    $parts [] = self::json_encode ( $value ); /* :RECURSION: */
                    else
                        $parts [] = '"' . $key . '":' . self::json_encode ( $value ); /* :RECURSION: */
            } else {
                $str = '';
                if (! $is_list)
                    $str = '"' . $key . '":';
                    //Custom handling for multiple data types
                    if (!is_string ( $value ) && is_numeric ( $value ) && $value<2000000000)
                        $str .= $value; //Numbers
                        elseif ($value === false)
                        $str .= 'false'; //The booleans
                        elseif ($value === true)
                        $str .= 'true';
                        else
                            $str .= '"' . addslashes ( $value ) . '"'; //All other things
                            // :TODO: Is there any more datatype we should be in the lookout for? (Object?)
                            $parts [] = $str;
            }
        }
        $json = implode ( ',', $parts );
        if ($is_list)
            return '[' . $json . ']'; //Return numerical JSON
            return '{' . $json . '}'; //Return associative JSON
    }
1352219
public function encodeArray($data) {
        $d = array();
        foreach ($data as $key => $value) {
            if (is_string($key))
                $key = htmlspecialchars($key);
            if (is_string($value))
                $value = htmlspecialchars($value);
            elseif (is_array($value))
                $value = self::encodeArray($value);
            $d[$key] = $value;
        }
        return $d;
    }
315594
function implodeDataArray(data) {
	var dataArray = [], i, l;
	l = data.length;
	for(i = 0; i < l; i++) {
		dataArray.push(types.STRING);
		dataArray.push(encodeString(data[i]));
	}
	return implodeArray(dataArray);
}
140893
protected function htmlEntityEncode($item)
    {
        if (is_array($item)) {
            array_walk_recursive($item, function (&$value) {
                $value = htmlentities($value);
            });
        } else {
            $item = htmlentities($item);
        }

        return $item;
    }
1357166
private function decodeNonUtf8FromUtf8(array $data)
    {
        $encodedKeys = [];
        if (isset($data[$this->encodingAnnotation])) {
            $encodedKeys = $data[$this->encodingAnnotation];
            unset($data[$this->encodingAnnotation]);
        }

        $decodedData = [];
        foreach ($data as $key => $value) {
            if (is_array($value)) {
                $value = $this->decodeNonUtf8FromUtf8($value);
            }

            if (isset($encodedKeys[$key])) {
                $originalKey = $key;
                if ($encodedKeys[$key] & static::KEY_UTF8ENCODED) {
                    $key = mb_convert_encoding($key, '8bit', 'UTF-8');
                }
                if ($encodedKeys[$originalKey] & static::VALUE_UTF8ENCODED) {
                    $value = mb_convert_encoding($value, '8bit', 'UTF-8');
                }
            }

            $decodedData[$key] = $value;
        }

        return $decodedData;
    }
113523
public function encodeUtf8($data)
    {
        if ($data === null || $data === '') {
            return $data;
        }
        if (is_array($data)) {
            foreach ($data as $key => $value) {
                $data[$key] = $this->encodeUtf8($value);
            }

            return $data;
        } else {
            if (!mb_check_encoding($data, 'UTF-8')) {
                return mb_convert_encoding($data, 'UTF-8');
            }

            return $data;
        }
    }
1131456
function encode(data) {
  $.checkArgument(data instanceof Array, 'Must be Array');
  var base32 = '';
  for (var i=0; i<data.length; i++) {
    var value = data[i];
    $.checkArgument(0 <= value && value < 32, 'Non-base32, value ' + value);
    base32 += CHARSET[value];
  }
  return base32;
}
830265
private function htmlEncode($data)
    {
        if (empty($data)) {
            return null;
        }

        $safeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890._-";

        $result = "";
        for ($i = 0; $i < strlen($data); $i++) {
            if (strstr($safeChars, $data[$i])) {
                $result .= $data[$i];
            } else if ("7F" >= $var = bin2hex(substr($data, $i, 1))) {
                $result .= "&#x" . $var . ";";
            } else {
                $result .= $data[$i];
            }
        }

        return $result;
    }
14247
static function vcard_encode($data)
    {
        foreach ((array)$data as $type => $entries) {
            // valid N has 5 properties
            while ($type == "N" && is_array($entries[0]) && count($entries[0]) < 5) {
                $entries[0][] = "";
            }

            // make sure FN is not empty (required by RFC2426)
            if ($type == "FN" && empty($entries)) {
                $entries[0] = $data['EMAIL'][0][0];
            }

            foreach ((array)$entries as $entry) {
                $attr = '';
                if (is_array($entry)) {
                    $value = array();
                    foreach ($entry as $attrname => $attrvalues) {
                        if (is_int($attrname)) {
                            if (!empty($entry['base64']) || $entry['encoding'] == 'B') {
                                $attrvalues = base64_encode($attrvalues);
                            }
                            $value[] = $attrvalues;
                        }
                        else if (is_bool($attrvalues)) {
                            // true means just a tag, not tag=value, as in PHOTO;BASE64:...
                            if ($attrvalues) {
                                // vCard v3 uses ENCODING=b (#1489183)
                                if ($attrname == 'base64') {
                                    $attr .= ";ENCODING=b";
                                }
                                else {
                                    $attr .= strtoupper(";$attrname");
                                }
                            }
                        }
                        else {
                            foreach ((array)$attrvalues as $attrvalue) {
                                $attr .= strtoupper(";$attrname=") . self::vcard_quote($attrvalue, ',');
                            }
                        }
                    }
                }
                else {
                    $value = $entry;
                }

                // skip empty entries
                if (self::is_empty($value)) {
                    continue;
                }

                $vcard .= self::vcard_quote($type) . $attr . ':' . self::vcard_quote($value) . self::$eol;
            }
        }

        return 'BEGIN:VCARD' . self::$eol . 'VERSION:3.0' . self::$eol . $vcard . 'END:VCARD';
    }
67857
function htmlEncode($html, $in = null, $out = null, $mode = 'auto')
    {
        $valid = array(
            'auto',
            'iconv',
            'mb_convert_encoding'
        );
        if (! isset($out)) {
            $out = 'UTF-8';
        }
        if (! in_array($mode, $valid)) {
            user_error('invalid mode, mode=' . $mode, E_USER_ERROR);
        }
        $if = function_exists('mb_convert_encoding');
        $if = $if && ($mode == 'auto' || $mode == 'mb_convert_encoding');
        $func = null;
        if (function_exists('iconv') && ($mode == 'auto' || $mode == 'iconv')) {
            $func = 'iconv';
        } elseif ($if) {
            $func = 'mb_convert_encoding';
        } else {
            user_error('encode failed, php extension not found', E_USER_ERROR);
        }
        $pattern = '/(<meta[^>]*?charset=(["\']?))([a-z\d_\-]*)(\2[^>]*?>)/is';
        if (! isset($in)) {
            $n = preg_match($pattern, $html, $in);
            if ($n > 0) {
                $in = $in[3];
            } else {
                if (function_exists('mb_detect_encoding')) {
                    $in = mb_detect_encoding($html);
                } else {
                    $in = null;
                }
            }
        }
        if (isset($in)) {
            $old = error_reporting(error_reporting() & ~ E_NOTICE);
            $html = call_user_func($func, $in, $out . '//IGNORE', $html);
            error_reporting($old);
            $html = preg_replace($pattern, "\\1$out\\4", $html, 1);
        } else {
            user_error('source encoding is unknown', E_USER_ERROR);
        }
        return $html;
    }