PHP数组编码变换
PHP数组编码转换
因为一些特殊字符的显示效果的原因不得不把习惯的utf-8工程改成了GBK,由于使用了ajax技术,又涉及到了老问题――编码转换。
一些表单验证需要返回json数据,php的json_encode函数只支持utf-8编码,无奈只得iconv了,需要达到的效果是GBK数组转换成utf-8数组传给json_encode函数。
最开始的思路,将数组序列化后用iconv函数转换编码,之后再反序列化,代码如下:
unserialize(iconv('gbk','utf-8',serialize($array)));
得到的结果是空白,后来想起来配置文件里设置了默认编码 ini_set('default_charset', 'gbk'); 这样用gbk反序列化utf-8的字符串肯定不好用了,此处在序列化和反序列化之间加个ini_set('default_charset', 'utf-8'); 应该也是可以的,但这么弄总觉得有点别扭,因为是全局的编码设置,很容易导致其他地方的编码问题,比如数据库操作。那么换个思路,用构建数组原型的序列化方法,借助var_export函数,最终函数如下:
function array_iconv($in_charset,$out_charset,$arr){ return eval('return '.iconv($in_charset,$out_charset,var_export($arr,true).';')); }
原理很简单 var_export设置第二个参数为true,返回数组原型字符串,将字符串转换为utf-8编码,之后再用eval来执行返回(类似匿名函数?),至此完美解决问题。
后续:后来在网上搜了下资料,看有没有更好的方法,找到的都大同小异,都是利用递归调用iconv的方式,如果数组元素过多或者维数多一些,性能上肯定不怎么样了,更好的是原生代码的方式,不需要考虑是N维数组还是关联数组,一切都已经自动完成,保证数组转换前后数据一致。从代码的长短以及循环和原生方法的比较上,相信大家已经有了选择。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Go language encoding analysis: UTF-8 and GBK comparison In the Go language, processing string encoding is one of the common tasks. Among them, UTF-8 and GBK are two commonly used character encoding methods. This article will conduct a detailed comparison between UTF-8 and GBK, discuss their differences and usage, and attach specific code examples. 1. Introduction to UTF-8 and GBK UTF-8: UTF-8 is a variable-length Unicode encoding method that can represent characters in almost all languages in the world. UTF-8

'Therequestedmoduledoesnotprovideanexportnamed'Error appears in VueCli – how to solve it? During the development of the Vue project, we may encounter the error message 'Therequestedmoduledoesnotprovideanexportnamed'. This error message usually appears when introducing third-party components

In the process of text processing, it is a common requirement to convert strings in different encoding formats. The iconv (InternationalizationConversion) function provided in the PHP language can meet this need very conveniently. This article will introduce the use of iconv function in detail from the following aspects: Definition of iconv function and introduction to common parameters Example demonstration: Convert GBK encoded string to UTF-8 encoded string Example demonstration: Convert UTF

iconv-fencoding[-tencoding][inputfile]...[Function] Converts the contents of a given file from one encoding to another. [Description]-fencoding: Convert characters from encoding to encoding. -tencoding: Convert characters to encoding. -l: List the known set of encoded characters -ofile: Specify the output file -c: Ignore illegal characters in the output -s: Suppress warning messages, but not error messages --verbose: Display progress information -f and -t can The specified legal characters are listed in the command with the -l option. [Example]* List currently supported character encodings

The difference between export and export default is that the export keyword is used to export one or more variables, functions, or classes, while the export default keyword is used to export a default variable, function, or class. In other modules, you can use the import keyword to import these exported variables, functions, or classes.

1.export function //Export file interface publicStringexport(){returnthis.myExport(exportList);}2.Export column name privateStringmyExport(Listlist){com.bronzesoft.power.tools.json.JSONObjectinfo=newcom.bronzesoft.power.tools .json.JSONObject();try{ListheadList=newArrayList(Arrays.asList(&
![[Recommended] Common methods to configure Linux environment variables!](https://img.php.cn/upload/article/000/887/227/170831918220244.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
In Linux systems, environment variables are an important mechanism for storing information about the system's operating environment. When we customize the installation of software, we usually need to configure environment variables. The following are several common methods for configuring Linux environment variables. I hope they will be helpful to you. 1. exportPATH Use the export command to directly modify the value of PATH and configure the way MySQL enters the environment variable: exportPATH=/home/uusama/mysql/bin:PATH# or put PATH in front exportPATH=PATH:/home/uusama/mysql/ bin Notes: Effective time: Effective immediately

1. Right-click the start menu and click Run. 2. Press control and press Enter to open the control panel > Clock and region > Region > Management method > Update operating system region settings > Check "Beta version: Use UnicodeUTF-8 to ensure global language support".
