CI Framework source Reading---------utf8.php
File address:./system/core/utf8.php
Main role: Provide UTF-8 coded environment support
The 1.__construct () constructor determines whether UTF8 is supported
(1) Log record Utf8 Class Initialized
(2) Call the $cfg in codeigniter.php into the current class.
(3) Judge if the regular expression supports the Utf8,iconv library is already installed, multibyte string function overloading is not enabled, the application word Descriptor is UTF8, so
(a) Logging: UTF-8 support Enabled.
(b) Define a constant utf8_enabled value of True
(c) If the mbstring extension is loaded, we set the internal code
(d) We'll set a tag so we don't have to use the extension_loaded () function multiple times
(4) Judge if the regular expression does not support UTF8 or the Iconv library is not installed or the multibyte string function overload is enabled or the application character set is not UTF8, then
(a) Logging: UTF-8 support Disabled
(b) Set constant utf8_enabled to False
2.clean_string () cleans up UTF8 encoded strings
(1) Determine if the string is not an ASCII code
(2) Use the Iconv function to transcode the string (see the Iconv function for details http://www.php.net/manual/zh/function.iconv.php)
(3) Return string
3.safe_ascii_for_xml () Removes all ASCII characters that can cause problems in XML, except for horizontal tabs, line feeds, and carriage returns.
(1) Call Remove_invisible_characters () directly to delete the invalid character and return.
Note: The Remove_invisible_characters function is defined in the common.php
4.convert_to_utf8 () Converts a string to UTF8 encoding
(1) If the ICONV function exists, use the Iconv conversion
(2) If the Mb_convert_encoding function is present, use the Mb_convert_encoding function to convert
(3) cannot convert return False if none of the above two functions exist
(4) If the conversion is complete, return the converted String
5._IS_ASCII () tests if a string is not an ASCII code
(1) Use regular blending to return the test results.
Item (' charset ') = = ' UTF-8 '//Application CharSet must is UTF-8) {log_message (' Debug ', ' UTF-8 support Enabled ');d efine (' Utf8_enabled ', TRUE);//Set internal internal encoding for multibyte multibyte string functions if necessary required//and set a flag so W E don ' t has to repeatedly multiple use extension_loaded ()//or Function_exists () if (extension_loaded (' mbstring ')) {define (' Mb_ ENABLED ', TRUE); mb_internal_encoding (' UTF-8 ');} Else{define (' mb_enabled ', FALSE);}} Else{log_message (' Debug ', ' UTF-8 support Disabled ');d efine (' utf8_enabled ', FALSE);}} --------------------------------/** * Clean UTF-8 strings * * ensures guarantee strings is UTF-8 * * @accesspublic * @paramst Ring * @returnstring */function clean_string ($str) {if ($this->_is_ascii ($str) = = = FALSE) {$str = @iconv (' UTF-8 ', ' Utf-8//ignore ', $str);} return $STR;} --------------------------------/** * Remove ASCII control characters * * Removes all ASCII control characters except H orizontal tabs, * line feeds, and carriage returns, as all others can cause *Problems in XML * * @accesspublic * @paramstring * @returnstring */function safe_ascii_for_xml ($str) {return remove_invisib Le_characters ($str, FALSE);} --------------------------------/** * Convert to UTF-8 * * Attempts attempt to convert a string to UTF-8 * * @accesspublic * @paramstring * @paramstring-input encoding * @returnstring */function Convert_to_utf8 ($str, $encoding) {if (Function_exi STS (' Iconv ')) {$str = @iconv ($encoding, ' UTF-8 ', $str);} ElseIf (function_exists (' mb_convert_encoding ')) {$str = @mb_convert_encoding ($str, ' UTF-8 ', $encoding);} Else{return FALSE;} return $STR;} --------------------------------/** * is ASCII? * * Tests If a string is standard 7-bit ASCII or not * * @accesspublic * @paramstring * @returnbool */function _is_ascii ($ STR) {return (Preg_match ('/[^\x00-\x7f]/s ', $str) = = 0);} --------------------------------}//End Utf8 class/* end of file utf8.php *//* location:./system/core/utf8.php */