/** * Sets the default character encoding to use for non-UTF8 strings. * * @param string $charset The default character encoding to use for non-UTF8 strings. * * @return string The previous charset. */ public function setCharset($charset) { $prev = $this->charset; $this->charsetConverter = 'fallback'; $charset = strtoupper($charset); $charset = null === $charset || 'UTF-8' === $charset || 'UTF8' === $charset ? 'CP1252' : $charset; $supported = true; set_error_handler(function () use(&$supported) { $supported = false; }); if (function_exists('mb_encoding_aliases') && mb_encoding_aliases($charset)) { $this->charset = $charset; $this->charsetConverter = 'mbstring'; } elseif (function_exists('iconv')) { $supported = true; iconv($charset, 'UTF-8', ''); if ($supported) { $this->charset = $charset; $this->charsetConverter = 'iconv'; } } if ('fallback' === $this->charsetConverter) { $this->charset = 'ISO-8859-1'; } restore_error_handler(); return $prev; }
public static function getSjisAliases() { $aliases = mb_encoding_aliases('SJIS'); if ($mimeName = mb_preferred_mime_name('SJIS')) { $aliases[] = $mimeName; } return $aliases; }
private function prepare_mb_list_encodings() { if (extension_loaded('mbstring')) { $this->_encoding = mb_internal_encoding(); $t_charset_list = mb_list_encodings(); // This function does not exist in version older then PHP 5.3.0 if (function_exists('mb_encoding_aliases')) { $t_encoding_aliases = array(); foreach ($t_charset_list as $t_value) { $t_encoding_aliases = array_merge($t_encoding_aliases, mb_encoding_aliases($t_value)); } $t_charset_list = array_merge($t_charset_list, $t_encoding_aliases); } $r_charset_list = array(); foreach ($t_charset_list as $t_value) { $r_charset_list[strtolower($t_value)] = $t_value; } $this->_mb_list_encodings = $r_charset_list + $this->_mbstring_unsupportedcharsets; } }
/** * @return array */ private static function getSupportedEncodings() { if (self::$supportedEncodings !== null) { return self::$supportedEncodings; } $hasAliasesFunction = function_exists('mb_encoding_aliases'); self::$supportedEncodings = []; foreach (mb_list_encodings() as $encoding) { $encoding = strtolower($encoding); if ($encoding === 'utf-8' or $encoding === 'utf8') { continue; } self::$supportedEncodings[] = $encoding; if ($hasAliasesFunction) { foreach (mb_encoding_aliases($encoding) as $encodingAlias) { $encodingAlias = strtolower($encodingAlias); self::$supportedEncodings[] = $encodingAlias; } } } return self::$supportedEncodings; }
<?php mb_encoding_aliases(); $list = mb_encoding_aliases("ASCII"); sort($list); var_dump($list); var_dump(mb_encoding_aliases("7bit")); var_dump(mb_encoding_aliases("8bit"));
function mb_list_lowerencodings() { // Encoding not listed static $enc = array('gb2312', 'gb18030'); if (count($enc) == 2) { foreach (mb_list_encodings() as $encoding) { $enc[] = Toolbox::strtolower($encoding); $aliases = mb_encoding_aliases($encoding); foreach ($aliases as $e) { $enc[] = Toolbox::strtolower($e); } } } return $enc; }
public final function init() { if ($this->isInit) { return $this; } $env = KE_APP_ENV; // 加载配置 import(["{$this->root}/config/common.php", "{$this->root}/config/{$env}.php"]); if (KE_APP_MODE === KE_WEB) { $this->httpRewrite = (bool) $this->httpRewrite; if (empty($this->httpBase)) { $target = dirname($_SERVER['SCRIPT_NAME']); if ($target === '\\') { $target = '/'; } $this->httpBase = compare_path(KE_REQUEST_PATH, $target, KE_DS_UNIX); } elseif ($this->httpBase !== '/') { $this->httpBase = purge_path($this->httpBase, KE_PATH_DOT_REMOVE ^ KE_PATH_LEFT_TRIM, KE_DS_UNIX); } // 上面的过滤,无论如何,过滤出来的httpBase都为没有首位的/的路径,如:path/dir/dir if (empty($this->httpBase)) { $this->httpBase = '/'; } elseif ($this->httpBase !== '/') { $this->httpBase = '/' . $this->httpBase . '/'; } // 如果不指定重写,则httpBase应该是基于一个php文件为基础的 if (!$this->httpRewrite) { $this->httpBase .= KE_SCRIPT_FILE; } define('KE_HTTP_BASE', $this->httpBase); define('KE_HTTP_REWRITE', (bool) $this->httpRewrite); } ///////////////////////////////////////////////////////////////////////////// // p2:填充当前的APP实例的数据 ///////////////////////////////////////////////////////////////////////////// // 初始化项目的名称 => 不应为空,也必须是一个字符串 if (empty($this->name) || !is_string($this->name)) { $this->name = KE_APP_DIR; } // 一个App的完整摘要 $summary = sprintf('%s(%s,%s,%s)', $this->name, KE_APP_ENV, KE_REQUEST_HOST, $this->root); // 项目的hash,基于完整摘要生成,而非基于用户设置的项目名称 // hash,主要用于服务器缓存识别不同的项目时使用 // 比如memcached,key为user.10,而这个项目的存储则应该是:$flag.user.10,来避免项目和项目之间的数据混串 $hash = hash('crc32b', $summary); // 真正用于显示的项目名称,包含项目名称、环境、hash $this->name = sprintf('%s(%s:%s)', $this->name, KE_APP_ENV, $hash); // 项目的基本加密混淆码 => 不应为空,也必须是一个字符串,且必须不小于32长度 if (empty($this->salt) || !is_string($this->salt) || strlen($this->salt) < 32) { $salt = $summary; } else { $salt = $this->salt; } define('KE_APP_NAME', $this->name); define('KE_APP_HASH', $hash); define('KE_APP_SALT', hash('sha512', $salt, true)); // 敏感数据还是清空为妙 $this->salt = null; // http验证字段,如果没指定,就只好使用一个统一的了 if (empty($this->httpSecurityField) || !is_string($this->httpSecurityField)) { $this->httpSecurityField = 'ke_http'; } if (empty($this->httpSecuritySessionField) || !is_string($this->httpSecuritySessionField)) { $this->httpSecuritySessionField = 'ke_security_reference'; } // http验证字段的加密混淆码 if (empty($this->httpSecuritySalt) || !is_string($this->httpSecuritySalt)) { $this->httpSecuritySalt = "{$this->name}:{$this->httpSecurityField}"; } $this->httpSecuritySalt = $this->hash($this->httpSecuritySalt); define('KE_HTTP_SECURITY_FIELD', $this->httpSecurityField); define('KE_HTTP_SECURITY_SALT', $this->httpSecuritySalt); define('KE_HTTP_SECURITY_SESS_FIELD', $this->httpSecuritySessionField); // 敏感数据还是清空为妙 $this->httpSecuritySalt = null; // 检查httpCharset if (empty($this->encoding) || false === @mb_encoding_aliases($this->encoding)) { $this->encoding = 'UTF-8'; } if (!empty($this->encodingOrder)) { if (is_string($this->encodingOrder)) { $this->encodingOrder = explode(',', $this->encodingOrder); } if (is_array($this->encodingOrder)) { $list = ['ASCII']; foreach ($this->encodingOrder as $encoding) { $encoding = strtoupper(trim($encoding)); if (empty($encoding) || $encoding === 'ASCII' || $encoding === $this->encoding) { continue; } $list[] = $encoding; } $list[] = $this->encoding; mb_detect_order($list); } } // 时区 if (empty($this->timezone) || false === @date_default_timezone_set($this->timezone)) { $this->timezone = 'Asia/Shanghai'; date_default_timezone_set($this->timezone); } define('KE_APP_TIMEZONE', $this->timezone); define('KE_APP_ENCODING', $this->encoding); // 系统的配置 ini_set('default_charset', KE_APP_ENCODING); ini_set('default_mimetype', 'text/html'); mb_internal_encoding(KE_APP_ENCODING); mb_http_output(KE_APP_ENCODING); $this->isInit = true; call_user_func([$this, 'on' . KE_APP_ENV]); register_shutdown_function(function () { $this->onExiting(); }); return $this; }
public function aliases($string = '') { if (!isCharset($string)) { return Error::set('Error', 'charsetParameter', '1.(string)'); } return mb_encoding_aliases($string); }
public function encodingAliases(string $string) : array { return mb_encoding_aliases($string); }
echo "<option value=\"{$encode}\" selected>{$encode}</option>\n"; } else { echo "<option value=\"{$encode}\">{$encode}</option>\n"; } } } } echo "</select>\n"; echo "<br />\n"; echo "输出编码:<select name=\"pc\">\n"; foreach ($sencode as $encode) { if ($encode == "auto" || $encode == "pass") { continue; } else { if (function_exists("mb_encoding_aliases")) { $alias = mb_encoding_aliases($encode); echo "\n<optgroup label=\"{$encode}\">\n"; if ($encode == "UTF-8") { echo "<option value=\"{$encode}\" selected>{$encode}</option>\n"; } else { echo "<option value=\"{$encode}\">{$encode}</option>\n"; } if (is_array($alias)) { if (count($alias) >= 1) { usort($alias, "___sortcmp"); foreach ($alias as $encodealias) { echo "<option value=\"{$encode}\">{$encodealias}</option>\n"; } } } echo "</optgroup>\n";
/** * Returns a mapping of accepted charset names to charsets. * * For example "UTF-8" and "utf8" are both valid charset names. * * @return array(string=>string) */ protected static function getCharsetNameMapping() { if (self::$namesToCharsets === null) { self::$namesToCharsets = array(); foreach (self::getCharsets() as $charset) { self::$namesToCharsets[$charset] = $charset; foreach (mb_encoding_aliases($charset) as $alias) { self::$namesToCharsets[$alias] = $charset; } } } return self::$namesToCharsets; }
/** * @return void */ protected static function buildEncodings() { $encodings = mb_list_encodings(); foreach ($encodings as $encoding) { self::addEncoding($encoding); foreach (mb_encoding_aliases($encoding) as $encoding) { self::addEncoding($encoding); } } }
/** * Convert string to UTF-8 charset * * @param string $string the input string * * @return string */ function profile_sync_convert_string_encoding($string) { if (function_exists('mb_convert_encoding')) { $source_encoding = mb_detect_encoding($string); if (!empty($source_encoding)) { $source_aliases = mb_encoding_aliases($source_encoding); return mb_convert_encoding($string, 'UTF-8', $source_aliases); } } // if no mbstring extension, we just try to convert to UTF-8 (from ISO-8859-1) return utf8_encode($string); }
public static function getAsciiCompatibleEncodings() { if (null === self::$ASCII_COMPATIBLE_ENCODINGS) { $ascii_chars = ''; foreach (range(0, 127) as $byte) { $ascii_chars .= chr($byte); } $compatible_encodings = []; foreach (mb_list_encodings() as $encoding) { $encoded = mb_convert_encoding($ascii_chars, $encoding); if ($encoded === $ascii_chars) { $compatible_encodings[] = strtolower($encoding); foreach (mb_encoding_aliases($encoding) as $alias) { $compatible_encodings[] = strtolower($alias); } } } self::$ASCII_COMPATIBLE_ENCODINGS = $compatible_encodings; } return self::$ASCII_COMPATIBLE_ENCODINGS; }
/** * Get aliases for this encoding * @return array */ public function getAliases() { return mb_encoding_aliases($this->encoding); }