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...