iconv() - iconv函数(字符编码转换库)
iconv()
(PHP 4 >= 4.0.5, PHP 5, PHP 7)
字符串按要求的字符编码来转换
说明
iconv(string $in_charset,string $out_charset,string $str): string将字符串$str从$in_charset转换编码到$out_charset。
参数
$in_charset输入的字符集。
$out_charset输出的字符集。
如果你在$out_charset后添加了字符串//TRANSLIT,将启用转写(transliteration)功能。这个意思是,当一个字符不能被目标字符集所表示时,它可以通过一个或多个形似的字符来近似表达。如果你添加了字符串//IGNORE,不能以目标字符集表达的字符将被默默丢弃。否则,会导致一个E_NOTICE
并返回FALSE
。
//TRANSLIT运行细节高度依赖于系统的 iconv()实现(参见ICONV_IMPL
)。据悉,某些系统上的实现会直接忽略//TRANSLIT,所以转换也有可能失败,$out_charset会是不合格的。
要转换的字符串。
返回值
返回转换后的字符串,或者在失败时返回FALSE
。
更新日志
版本 | 说明 |
---|---|
5.4.0 | 这个版本起,字符非法时候会返回FALSE ,除非在输出字符里指定了//IGNORE。在之前版本,它会返回一部分字符串。 |
范例
iconv()例子
以上例程的输出类似于:
Original : This is the Euro symbol '€'. TRANSLIT : This is the Euro symbol 'EUR'. IGNORE : This is the Euro symbol ''. Plain : Notice: iconv(): Detected an illegal character in input string in .\iconv-example.php on line 7
The "//ignore" option doesn't work with recent versions of the iconv library. So if you're having trouble with that option, you aren't alone. That means you can't currently use this function to filter invalid characters. Instead it silently fails and returns an empty string (or you'll get a notice but only if you have E_NOTICE enabled). This has been a known bug with a known solution for at least since 2009 years but no one seems to be willing to fix it (PHP must pass the -c option to iconv). It's still broken as of the latest release 5.4.3. https://bugs.php.net/bug.php?id=48147 https://bugs.php.net/bug.php?id=52211 https://bugs.php.net/bug.php?id=61484 [UPDATE 15-JUN-2012] Here's a workaround... ini_set('mbstring.substitute_character', "none"); $text= mb_convert_encoding($text, 'UTF-8', 'UTF-8'); That will strip invalid characters from UTF-8 strings (so that you can insert it into a database, etc.). Instead of "none" you can also use the value 32 if you want it to insert spaces in place of the invalid characters.
Please note that iconv('UTF-8', 'ASCII//TRANSLIT', ...) doesn't work properly when locale category LC_CTYPE is set to C or POSIX. You must choose another locale otherwise all non-ASCII characters will be replaced with question marks. This is at least true with glibc 2.5. Example:
Interestingly, setting different target locales results in different, yet appropriate, transliterations. For example:
to test different combinations of convertions between charsets (when we don't know the source charset and what is the convenient destination charset) this is an example : then after displaying, you use the $i$j that shows good displaying. NB: you can add other charsets to $tab to test other cases.
If you are getting question-marks in your iconv output when transliterating, be sure to 'setlocale' to something your system supports. Some PHP CMS's will default setlocale to 'C', this can be a problem. use the "locale" command to find out a list.. $ locale -a C en_AU.utf8 POSIX
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)