📄 utf_tools.php
字号:
* Make a string's first character uppercase* * @author Harry Fuecks* @param string* @return string with first character as upper case (if applicable)*/function utf8_ucfirst($str){ switch (utf8_strlen($str)) { case 0: return ''; break; case 1: return utf8_strtoupper($str); break; default: preg_match('/^(.{1})(.*)$/us', $str, $matches); return utf8_strtoupper($matches[1]) . $matches[2]; break; }}/*** Recode a string to UTF-8** If the encoding is not supported, the string is returned as-is** @param string $string Original string* @param string $encoding Original encoding (lowered)* @return string The string, encoded in UTF-8*/function utf8_recode($string, $encoding){ $encoding = strtolower($encoding); if ($encoding == 'utf-8' || !is_string($string) || !isset($string[0])) { return $string; } // start with something simple if ($encoding == 'iso-8859-1') { return utf8_encode($string); } // First, try iconv() if (function_exists('iconv')) { $ret = @iconv($encoding, 'utf-8', $string); if (isset($ret[0])) { return $ret; } } // Try the mb_string extension if (function_exists('mb_convert_encoding')) { $ret = @mb_convert_encoding($string, 'utf-8', $encoding); if (isset($ret[0])) { return $ret; } } // Try the recode extension if (function_exists('recode_string')) { $ret = @recode_string($encoding . '..utf-8', $string); if (isset($ret[0])) { return $ret; } } // If nothing works, check if we have a custom transcoder available if (!preg_match('#^[a-z0-9\\-]+$#', $encoding)) { // Make sure the encoding name is alphanumeric, we don't want it to be abused into loading arbitrary files trigger_error('Unknown encoding: ' . $encoding, E_USER_ERROR); } global $phpbb_root_path; if (!file_exists($phpbb_root_path . 'includes/utf/data/')) { return $string; } die('Finish me!! ' . basename(__FILE__) . ' at line ' . __LINE__);}/*** Replace all UTF-8 chars that are not in ASCII with their NCR** @param string $text UTF-8 string in NFC* @return string ASCII string using NCRs for non-ASCII chars*/function utf8_encode_ncr($text){ return preg_replace_callback('#[\\xC2-\\xF4][\\x80-\\xBF]{1,3}#', 'utf8_encode_ncr_callback', $text);}/*** Callback used in encode_ncr()** Takes a UTF-8 char and replaces it with its NCR. Attention, $m is an array** @param array $m 0-based numerically indexed array passed by preg_replace_callback()* @return string A HTML NCR if the character is valid, or the original string otherwise*/function utf8_encode_ncr_callback($m){ return '&#' . utf8_ord($m[0]) . ';';}/*** Enter description here...** @param string $chr UTF-8 char* @return integer UNICODE code point*/function utf8_ord($chr){ switch (strlen($chr)) { case 1: return ord($chr); break; case 2: return ((ord($chr[0]) & 0x1F) << 6) | (ord($chr[1]) & 0x3F); break; case 3: return ((ord($chr[0]) & 0x0F) << 12) | ((ord($chr[1]) & 0x3F) << 6) | (ord($chr[2]) & 0x3F); break; case 4: return ((ord($chr[0]) & 0x07) << 18) | ((ord($chr[1]) & 0x3F) << 12) | ((ord($chr[2]) & 0x3F) << 6) | (ord($chr[3]) & 0x3F); break; default: return $chr; }}/*** Converts an NCR to a UTF-8 char** @param int $cp UNICODE code point* @return string UTF-8 char*/function utf8_chr($cp){ if ($cp > 0xFFFF) { return chr(0xF0 | ($cp >> 18)) . chr(0x80 | (($cp >> 12) & 0x3F)) . chr(0x80 | (($cp >> 6) & 0x3F)) . chr(0x80 | ($cp & 0x3F)); } else if ($cp > 0x7FF) { return chr(0xE0 | ($cp >> 12)) . chr(0x80 | (($cp >> 6) & 0x3F)) . chr(0x80 | ($cp & 0x3F)); } else if ($cp > 0x7F) { return chr(0xC0 | ($cp >> 6)) . chr(0x80 | ($cp & 0x3F)); } else { return chr($cp); }}/*** Convert Numeric Character References to UTF-8 chars** Notes:* - we do not convert NCRs recursively, if you pass &#38; it will return &* - we DO NOT check for the existence of the Unicode characters, therefore an entity may be converted to an inexistent codepoint** @param string $text String to convert, encoded in UTF-8 (no normal form required)* @return string UTF-8 string where NCRs have been replaced with the actual chars*/function utf8_decode_ncr($text){ return preg_replace_callback('/&#([0-9]{1,6}|x[0-9A-F]{1,5});/i', 'utf8_decode_ncr_callback', $text);}/*** Callback used in decode_ncr()** Takes a NCR (in decimal or hexadecimal) and returns a UTF-8 char. Attention, $m is an array.* It will ignore most of invalid NCRs, but not all!** @param array $m 0-based numerically indexed array passed by preg_replace_callback()* @return string UTF-8 char*/function utf8_decode_ncr_callback($m){ $cp = (strncasecmp($m[1], 'x', 1)) ? $m[1] : hexdec(substr($m[1], 1)); return utf8_chr($cp);}/*** Takes an array of ints representing the Unicode characters and returns* a UTF-8 string.** @param string $text text to be case folded* @param string $option determines how we will fold the cases* @return string case folded text*/function utf8_case_fold($text, $option = 'full'){ static $uniarray = array(); global $phpbb_root_path, $phpEx; // common is always set if (!isset($uniarray['c'])) { $uniarray['c'] = include($phpbb_root_path . 'includes/utf/data/case_fold_c.' . $phpEx); } // only set full if we need to if ($option === 'full' && !isset($uniarray['f'])) { $uniarray['f'] = include($phpbb_root_path . 'includes/utf/data/case_fold_f.' . $phpEx); } // only set simple if we need to if ($option !== 'full' && !isset($uniarray['s'])) { $uniarray['s'] = include($phpbb_root_path . 'includes/utf/data/case_fold_s.' . $phpEx); } $text = strtr($text, $uniarray['c']); if ($option === 'full') { $text = strtr($text, $uniarray['f']); } else { $text = strtr($text, $uniarray['s']); } return $text;}/*** A wrapper function for the normalizer which takes care of including the class if required and modifies the passed strings* to be in NFC (Normalization Form Composition).** @param mixed $strings a string or an array of strings to normalize* @return mixed the normalized content, preserving array keys if array given.*/function utf8_normalize_nfc($strings){ if (empty($strings)) { return $strings; } if (!class_exists('utf_normalizer')) { global $phpbb_root_path, $phpEx; include($phpbb_root_path . 'includes/utf/utf_normalizer.' . $phpEx); } if (!is_array($strings)) { utf_normalizer::nfc($strings); } else if (is_array($strings)) { foreach ($strings as $key => $string) { utf_normalizer::nfc($strings[$key]); } } return $strings;}/*** This function is used to generate a "clean" version of a string.* Clean means that it is a case insensitive form (case folding) and that it is normalized (NFC).* Additionally a homographs of one character are transformed into one specific character (preferably ASCII* if it is an ASCII character).** Please be aware that if you change something within this function or within* functions used here you need to rebuild/update the username_clean column in the users table. And all other* columns that store a clean string otherwise you will break this functionality.** @param string $text An unclean string, mabye user input (has to be valid UTF-8!)* @return string Cleaned up version of the input string*/function utf8_clean_string($text){ $text = utf8_case_fold($text); if (!class_exists('utf_normalizer')) { global $phpbb_root_path, $phpEx; include($phpbb_root_path . 'includes/utf/utf_normalizer.' . $phpEx); } utf_normalizer::nfc($text); static $homographs = array( // cyrllic "\xD0\xB0" => "\x61", "\xD0\xB5" => "\x65", "\xD0\xBE" => "\x6F", "\xD1\x80" => "\x70", "\xD1\x81" => "\x63", "\xD1\x83" => "\x79", "\xD1\x85" => "\x78", "\xD1\x95" => "\x73", "\xD1\x96" => "\x69", "\xD1\x98" => "\x6A", "\xD2\xBB" => "\x68", // greek "\xCE\xB1" => "\x61", "\xCE\xBF" => "\x6F", // other "\xC2\xA1" => "\x69", ); $text = strtr($text, $homographs); return $text;}?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -