This article mainly and everyone to share the PHP code conversion garbled How to solve, combined with text and code, hope to help everyone.
Iconv Detailed:
iconv-strings are converted As required character encodings
Iconv has a bug, encountered some rare words will not be able to convert, of course, when configuring the second parameter, you can make up a little bit of the default flaw, not to be able to convert is truncated, use the following
Iconv ("Utf-8″," Gb2312//ignore ", $data);
When this encounters an uncommon character conversion failure, it ignores the failure and continues to convert the following content.
Iconvstring iconv (String $in _charset, String $out _charset, string $str) First argument: Content original encoding second parameter: Target encoding third parameter: string function to go return string < php$instr = ' test ';//GBK Turn utf-8$outstr = iconv (' GBK ', ' utf-8′, $instr);? >
return value
Returns the converted string, or FALSE on failure.
mb_convert_encoding Detailed:
To ensure the success of the conversion, we can use another conversion function
Mb_convert_encoding, this function is not very efficient, and this function can also omit the third parameter, automatically identify the content encoding, but it is best not to use, affect efficiency, but also need to pay attention to the mb_convert_encoding and Iconv parameter order is not the same , be sure to pay attention.
A simple usage of two functions:
Mb_convert_encodingstring mb_convert_encoding (String $str, String $to _encoding [, Mixed $from _encoding]) First argument: the string to be processed The second parameter: the third parameter of the target encoding: the contents of the original encoding, which can be an array or a comma-delimited list of enumerations <?php$instr = ' test ';//GBK to Utf-8$outstr = mb_convert_encoding ($ InStr, ' UTF-8 ', ' GBK ',); $str = mb_convert_encoding ($instr, "Ucs-2le", "JIS, Eucjp-win, Sjis-win");? >
Personal advice to meet transcoding problems when using mb_convert_encoding comparative insurance.
Mb_convert_variables
mb_convert_variables- converting the character encoding of one or more variables
Mb_convert_variables ($to _encoding, $from _encoding, & $vars [, Mixed &$ ...]
Converts the encoding of the variable VARs from from_encoding to the encoded to_encoding.
Mb_convert_variables () will stitch the string in the variable array or object to detect the encoding, because the detection of short strings often fails. Therefore, you cannot mix encodings in an array or an object.
To_encoding Converts a string into this encoding. From_encoding can be specified as an array or a comma-delimited string, which attempts to detect the encoding based on from-coding. When From_encoding is omitted, the Detect_order is used. VARs is a reference to the variable to be converted. Parameters can accept the type of String, Array, and Object. Mb_convert_variables () assumes that all parameters have the same encoding. The extra VARs.
Return value: Returns the character encoding before conversion on success, and FALSE on failure.
Example: <?php/* conversion variable $post 1, $post 2 encoded as internal (internal) code */$interenc = mb_internal_encoding (); $inputenc = Mb_convert_ Variables ($interenc, "Ascii,utf-8,sjis-win", $post 1, $post 2);? >
mb_internal_encoding
mb_internal_encoding- set/Get internal character encoding
Mixed mb_internal_encoding ([string $encoding = Mb_internal_encoding ()])
Parameters: Encoding character encoding name used for HTTP input character encoding conversion, HTTP output character encoding conversion, Mbstring module series function number character encoding conversion default encoding. Return value: Returns TRUE on success if encoding is set, or FALSE on failure. The character encoding for multibyte regex are not changed. If encoding is omitted, the current character encoding name is returned.
<?php/* set the internal character encoding to UTF-8 */mb_internal_encoding ("UTF-8");/* Displays the current internal character encoding */echo mb_internal_encoding (); >
Mb_detect_encoding Detailed:
mb_detect_encoding- encoding of detected characters
String mb_detect_encoding (String $str [, mixed $encoding _list = Mb_detect_order () [, bool $strict = false]])
Detects the encoding of the string str.
the string to check for parameter str. Encoding_list is a list of character encodings. The encoding order can be specified by an array or comma-delimited list string. If Encoding_list is omitted, the Detect_order will be used. Strict strict Specifies whether to strictly detect the encoding. The default is FALSE. The return value detects the character encoding, or returns FALSE if the encoding of the specified string cannot be detected.
encode a string when the string encoding is unknown:
1, regardless of the string encoding is what, convert to GBK
function Getsafestr ($str) { $s 1 = iconv (' utf-8 ', ' Gbk//ignore ', $str); $s 0 = iconv (' GBK ', ' utf-8//ignore ', $s 1); if ($s 0 = = $str) { return $s 1; } else{ return $str; }}
2, regardless of the string encoding is what, convert to Utf-8
function Getsafestr ($str) { $s 1 = iconv (' GBK ', ' Utf-8//ignore ', $str); $s 0 = iconv (' utf-8 ', ' gbk//ignore ', $s 1); if ($s 0 = = $str) { return $s 1; } else{ return $str; }}
Get the string encoding method: