search
Homephp教程php手册php中iconv函数的一个小bug

iconv转换字符集很好用,但是有时候你会发现iconv转换的时候会返回false或者空字符串,严格说来这算不上是iconv的问题,这其实是字符集的问题,但是实际编码中应该算是iconv的bug了。 你可以试试把以下这段字符串用iconv转换一下看看效果。 $str="www.bloggu

iconv转换字符集很好用,但是有时候你会发现iconv转换的时候会返回false或者空字符串,严格说来这算不上是iconv的问题,这其实是字符集的问题,但是实际编码中应该算是iconv的bug了。

你可以试试把以下这段字符串用iconv转换一下看看效果。

 $str="www.blogguy.cn,ok!the string is € .我要转换他!⊙●○①⊕◎Θ⊙¤㊣";
 echo '没有任何参数 : ', iconv("utf-8", "gbk", $str)."
";

在我的电脑中返回的结果是第一个特殊字符以后的字符都不能显示。

iconv有两个参数:分别是TRANSLIT 和IGNORE   ,分别的含义是TRANSLIT 表示如果目标编码里面不能翻译原编码,那么就直接找一个类似的最相近的字符或者字符串替代,例如€可能会被替换为EUR;而IGNORE   遇到目标字符集不能翻译的原始字符则直接跳过忽略,不返回false。

使用方法如下:

$infocontent=iconv("utf-8","gbk//IGNORE",$infocontent);

或者

$infocontent=iconv("utf-8","gbk//TRANSLIT",$infocontent);

鉴于此,我认为iconv应该提供一个默认的参数IGNORE,避免开发者使用不善时出现返回空字符串的情况。

一点小记录。

 

http://www.blogguy.cn/show-797-1.html

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function