Home >Backend Development >PHP Problem >What should I do if the iconv function in php reports an error?

What should I do if the iconv function in php reports an error?

WBOY
WBOYOriginal
2022-04-13 10:26:002653browse

Method: 1. Add "//IGNORE" after the second parameter of the function, the syntax is "iconv("UTF-8","GB2312//IGNORE",..)"; 2. Replace gb2312 with "utf-8", the syntax is "iconv("GB2312","UTF-8",..)".

What should I do if the iconv function in php reports an error?

The operating environment of this article: Windows 10 system, PHP version 7.1, Dell G3 computer

What to do if the iconv function reports an error in php

iconv() Introduction

The iconv function can convert a known character set file into another known character set file. For example: Convert from GB2312 to UTF-8.

The iconv function is built-in in php5, and the GB character set is turned on by default.

iconv() error

Iconv will make an error when converting the character "-" to gb2312. The solution is to add "" after the encoding that needs to be converted. //IGNORE", that is, after the second parameter of the iconv function. As follows:

      iconv("UTF-8", "GB2312//IGNORE", $data)

ignore means to ignore errors during conversion. Without the ignore parameter, all strings following this character cannot be saved.

The iconv function library in PHP can complete conversion between various character sets and is an indispensable basic function library in PHP programming; but sometimes iconv will be less able to transcode some data for no reason. For example, an error will occur when converting the characters "— or Traditional Chinese" to gb2312.

Let’s take a look at the usage of this function.

The simplest application is to replace gb2312 with utf-8:

`$text``=iconv(``"GB2312"``,``"UTF-8"``,``$text``);`

In the process of using $text=iconv("UTF-8","GB2312",$text), if When encountering some special characters, such as "— or Traditional Chinese", "." in English names, etc., the conversion will be interrupted. The text after these characters cannot be converted further.

To solve this problem, you can use the following code to achieve it:

`$text``=iconv(``"UTF-8"``,``"GBK"``,``$text``);`

You read that right, it’s that simple, just write GBK instead of using gb2312.

There is another method, the second parameter, add //IGNORE, ignore the error, as follows:

`iconv(``"UTF-8"``,``"GB2312//IGNORE"``,``$data``);`

There is no specific comparison between these two methods, I feel that the first one (GBK instead of gb2312 ) method is better.

Examples are as follows:

  <?php
    echo $str= ‘你好,这里是卖咖啡!’;
    echo &#39;<br />&#39;;
    echo iconv(&#39;GB2312&#39;, &#39;UTF-8&#39;, $str); //将字符串的编码从GB2312转到UTF-8
    echo &#39;<br />&#39;;
    echo iconv_substr($str, 1, 1, &#39;UTF-8&#39;); //按字符个数截取而非字节
    print_r(iconv_get_encoding()); //得到当前页面编码信息
    echo iconv_strlen($str, &#39;UTF-8&#39;); //得到设定编码的字符串长度
  ?>

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What should I do if the iconv function in php reports an error?. For more information, please follow other related articles on the PHP Chinese website!

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