PHP string Encoding Conversion

Source: Internet
Author: User
In the Web development to often carry out the conversion of string encoding, the slightest careless will cause garbled. So it can be said that the coding problem is a big problem. After a search, we found a code for the conversion of string encoding, and then made a simple packaging, the use of more convenient.

<?phpfunction Phpcharset ($data, $to) {    if (Is_array ($data)) {        foreach ($data as $key + = $val) {            $data [$ Key] = Phpcharset ($val, $to);        }    } else {        $encode _array = Array (            ' ASCII ',            ' UTF-8 ',            ' GBK ',            ' GB2312 ',            ' BIG5 '        );        $encoded      = mb_detect_encoding ($data, $encode _array);        $to           = Strtoupper ($to);        if ($encoded! = $to) {            $data = mb_convert_encoding ($data, $to, $encoded);        }    }    return $data;} function ToUTF8 ($data) {    return Phpcharset ($data, ' UTF-8 ');} function TOGBK ($data) {    return Phpcharset ($data, ' GBK ');} function Toascii ($data) {    return Phpcharset ($data, ' ASCII ');} function toGB2312 ($data) {    return Phpcharset ($data, ' GB2312 ');} function ToBIG5 ($data) {    return Phpcharset ($data, ' BIG5 ');}? >
  • 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.