Home >Backend Development >PHP Tutorial >iconv and mb_convert_string string conversion

iconv and mb_convert_string string conversion

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-29 09:12:491302browse

I have been dealing with string conversion recently, and the two more commonly used ones are the two string conversions that come with PHP. Then I will use these two string encoding conversion functions in some scenarios


Use Scene:
Request: ajax POST request
Server encoding GBK
Page encoding GBK

Problem: Because the data sent by ajax requests are all encoded in utf-8 format, we must convert the utf-8 encoded data

Solution 1: Use iconv

<code><?php
<span>$postStr</span> = file_get_contents(<span>"file://input"</span>); <span>//</span> 将post的数据以字符流的形式读取
<span>$inCharset</span> = <span>"UTF-8"</span>;
<span>$outCharset</span> = <span>"GBK"</span>;
<span>$postStr</span> = iconv(<span>$inCharset</span>,<span>$outCharset</span>,<span>$postStr</span>);
<span>//</span> 将字符串转换为<span>$_POST</span>形式的数组
parse_str(<span>$postStr</span>,<span>$_post</span>);
</code>

to solve Method 2: Use mb_convert_encode()

<code><?php
<span>$postStr</span> = file_get_contents(<span>"file://input"</span>); <span>//</span> 将post的数据以字符流的形式读取
<span>$inCharset</span> = <span>"UTF-8"</span>;
<span>$outCharset</span> = <span>"GBK"</span>;
<span>$postStr</span> = mb_convert_encode(<span>$postStr</span>,<span>$outCharset</span>,<span>$inCharset</span>);

<span>//</span> 将字符串转换为<span>$_POST</span>形式的数组
parse_str(<span>$postStr</span>,<span>$_post</span>);
</code>

Both of the above two methods can be used to convert strings. However, one thing needs to be noted. If you convert the converted string back, you must not mix the two methods. Otherwise, Chinese characters may be Stage problems arise.

Example:

<code><?php
<span>$postStr</span> = file_get_contents(<span>"file://input"</span>); // 将post的数据以字符流的形式读取
<span>$inCharset</span> = <span>"UTF-8"</span>;
<span>$outCharset</span> = <span>"GBK"</span>;
<span>$postStr</span> = mb_convert_encode(<span>$postStr</span>,<span>$outCharset</span>,<span>$inCharset</span>);

// 转换为原来的字符串
<span>$postStr</span> = iconv(<span>$outCharset</span>,<span>$inCharset</span>.<span>"//IGNORE"</span>,<span>$postStr</span>);
// 如果源 <span>$postStr</span>为 UTF-<span>8</span>的 <span>'我是谁?'</span>
// 那么新的 <span>$postStr</span> 为 <span>'?'</span> ,如果不加 <span>"//IGNORE"</span> 结尾 则会跑出一个异常

</code>

So never nest two methods to convert each other.

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces iconv and mb_convert_string string conversion, including ajax content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn