Php code conversion _ PHP Tutorial

Source: Internet
Author: User
Php code conversion. Encoding and conversion this code is in the ThinkPHP framework and is often used. let's take a look at it separately. Functionauto_charset ($ fContents, $ from, $ to) {if (str encoding and conversion code is in the ThinkPHP framework. it feels very common, so let's take it out separately. let's take a look.

Function auto_charset ($ fContents, $ from = '', $ to = '')
{
If (strtoupper ($ from) === strtoupper ($ to) | empty ($ fContents) | (is_scalar ($ fContents )&&! Is_string ($ fContents ))){
// If the encoding is the same or the non-string scalar, the conversion is not performed.
Return $ fContents;
}
$ From = strtoupper ($ from) = 'utf8 '? 'Utf-8': $ from;
$ To = strtoupper ($ to) = 'utf8 '? 'Utf-8': $;
If (is_string ($ fContents )){
If (function_exists ('MB _ convert_encoding ')){
Return mb_convert_encoding ($ fContents, $ to, $ from );
} Elseif (function_exists ('iconv ')){
Return iconv ($ from, $ to, $ fContents );
} Else {
Exit ('conversion failed ');
Return $ fContents;
}
}
Elseif (is_array ($ fContents )){
Foreach ($ fContents as $ key => $ val ){
$ _ Key = auto_charset ($ key, $ from, $ );
$ FContents [$ _ key] = auto_charset ($ val, $ from, $ );
If ($ key! =$ _ Key ){
Unset ($ fContents [$ key]);
}
}
Return $ fContents;
}
Elseif (is_object ($ fContents )){
$ Vars = get_object_vars ($ fContents );
Foreach ($ vars as $ key => $ val ){
$ FContents-> $ key = auto_charset ($ val, $ from, $ );
}
Return $ fContents;
}
Else {
Return $ fContents;
}
}

The coding section is in the ThinkPHP framework and is often used. so let's take a look at it separately. Function auto_charset ($ fContents, $ from = '', $ to ='') {if (str...

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.