php:php automatically recognize character sets and complete transcoding _php tutorial

Source: Internet
Author: User
PHP automatically recognize the character set and complete transcoding because it uses character encoding is UTF-8 encoding, but if the other side of the blog using gb2312 encoding, post will appear garbled (unless the other party before the post-code conversion). It is necessary to do a coded check and conversion without ensuring that the other party is using UTF-8 encoding. Write a function to do this work, the principle is very simple, because GB2312/GBK is a Chinese two bytes, the two bytes is a range of values, and utf-8 in the Chinese characters are three bytes, also each byte has a value range. The English language is less than 128 and takes only one byte (except the full-width), regardless of the encoding. If it is the file form of the code check, you can also directly check utf-8 BOM information, about this aspect of things, you can see the TP Toolbox encoding conversion function, I wrote in the Appcodingswitch class more detailed comments. Not much to say, directly on the function, this function is used to check and transcode the string. File check with transcoding [PHP] function safeencoding ($string, $outEncoding = ' UTF-8 ') {$encoding = "UTF-8"; for ($i = 0; $i < strlen ($ string); $i + +) {if (ord ($string {$i}) < Continue), if (Ord ($string {$i}) & 224) = = 224) {//The first byte is judged by $char = $string {+ +) $i}; if (Ord ($char) & 128) = = 128) {///second byte is judged by $char = $string {+ + $i}; if (Ord ($char) & x = =) {$encoding = " UTF-8 "; Break }}} if ((Ord ($string {$i}) & 192) = = 192) {///The first byte is judged by $char = $string {+ + $i}; if (Ord ($char) & 128) = = 128) {/ /The second byte is judged by $encoding = "GB2312"; Break }}} if (Strtoupper ($encoding) = = Strtoupper ($outEncoding)) return $string; else Returniconv ($encoding, $outEncoding, $string); }

http://www.bkjia.com/PHPjc/477773.html www.bkjia.com true http://www.bkjia.com/PHPjc/477773.html techarticle PHP automatically recognize the character set and complete transcoding because it uses character encoding is UTF-8 encoding, but if the other party's blog using gb2312 encoding, post will appear garbled (unless ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.