Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial iconv函数和mb_convert_encoding函数转码的问题

iconv函数和mb_convert_encoding函数转码的问题

Jun 23, 2016 pm 02:02 PM

这两个函数各有特点,
1、iconv速度快,自然优先选择,但是他有个缺点,如果遇到自己不能转换的字符,就从那里截断。这就导致转码中内容被无故截断。
2、mb_convert_encoding函数效率比较低,但是他遇到无法转换的内容不会截断,这很大程度保留了内容的完整性。但是我发现比如内容有空格,转换出来的内容就有?符号,还是不够完美。

如何结合这两个函数对字符进行转码?

我的思路是这样的:
优先肯定使用iconv函数,因为效率高,而且是内置函数。discuz的转码函数也是优先使用此函数,但是他转码不完整的情况下,就要安排mb_convert_encoding去收拾残局了,问题来了,如何判断他转码不完整?


回复讨论(解决方案)

虽然 mb_convert_encoding函数 需要加载 php_mbstring 扩展
但加载了就不能说不是内置函数了

对于不可识别的字符,iconv 提供了两个开关供你选用,并不是一味的截断
//TRANSLIT 用相近的字符替代
//IGNORE 丢弃并继续

虽然 mb_convert_encoding函数 需要加载 php_mbstring 扩展
但加载了就不能说不是内置函数了

对于不可识别的字符,iconv 提供了两个开关供你选用,并不是一味的截断
//TRANSLIT 用相近的字符替代
//IGNORE 丢弃并继续

好像我也加了IGNORE 函数,但是有些内容转码还是不够理想。
换句话说,有时候用iconv 行,mb_convert_encoding不行,mb_convert_encoding行,iconv 又不行

虽然 mb_convert_encoding函数 需要加载 php_mbstring 扩展
但加载了就不能说不是内置函数了

对于不可识别的字符,iconv 提供了两个开关供你选用,并不是一味的截断
//TRANSLIT 用相近的字符替代
//IGNORE 丢弃并继续

试验了一下,IGNORE也许是可以绕过去的(之前遇到不能饶的),但是转换某些字符时,把字符丢失了。
比如把“?”符号的转换成gb2312时,iconv没转换过来,但是mb_convert_encoding转换过来了。所以二者各有优缺点,现在只是想怎样才能比较完美的结合起来使用。

Statement of this Website
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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

Announcement of 2025 PHP Situation Survey

See all articles