国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 php手冊 php iconv() 編碼轉(zhuǎn)換出錯 Detected an illegal character

php iconv() 編碼轉(zhuǎn)換出錯 Detected an illegal character

May 25, 2016 pm 04:53 PM
character iconv

原型:string iconv ( string $in_charset , string $out_charset , string $str )

特別是第二個參數(shù)說明:the output charset.

用iconv()轉(zhuǎn)換一個輸出字符編碼不支持的字符時,如iconv('utf-8', 'gb2312', 'www.phprm.com'),會遇到這樣的錯誤提示:notice: iconv() [function.iconv]: detected an illegal character in input string ...

因為gb2312表示的是簡體中文,不支持像"www.phprm.com"之類的更為復(fù)雜的漢字以及一些特殊字符,這當(dāng)然會報錯了,解決辦法有兩種:

1. 擴(kuò)大輸出字符編碼的范圍,如iconv('utf-8', 'gbk', 'www.phprm.com'),則可以正確地輸出,因為gbk支持的字符范圍更廣;

2. 在輸出的字符編碼字符串后面加上"http://ignore",如iconv('utf-8', 'gb2312//ignore', 'www.phprm.com'),這樣做其實是忽略了不能轉(zhuǎn)換的字符,避免了出錯但卻不能夠正確地輸出(即空白不、輸出)。

下面來看看關(guān)于php教程 iconv() : detected an illegal character in input string處理方法:

$str = iconv('utf-8', 'gbk//ignore', unescape(isset($_get['str'])? $_get['str']:''));

本地測試//ignore能忽略掉它不認(rèn)識的字接著往下轉(zhuǎn),并且不報錯,而//translit是截掉它不認(rèn)識的字及其后面的內(nèi)容,并且報錯。//ignore是我需要的。

在網(wǎng)上找到下面這篇文章,發(fā)現(xiàn)mb_convert_encoding也可以,但效率比iconv差,轉(zhuǎn)換字符串編碼iconv與mb_convert_encoding的區(qū)別:

iconv — convert string to requested character encoding(php 4 >= 4.0.5, php 5)

mb_convert_encoding — convert character encoding(php 4 >= 4.0.6, php 5)

用法:

string mb_convert_encoding ( string str, string to_encoding [, mixed from_encoding] )

需要先啟用 mbstring 擴(kuò)展庫,在 php.ini里將; extension=php_mbstring.dll 前面的 ; 去掉

string iconv ( string in_charset, string out_charset, string str )

注意:第二個參數(shù),除了可以指定要轉(zhuǎn)化到的編碼以外,還可以增加兩個后綴://translit 和 //ignore,其中://translit 會自動將不能直接轉(zhuǎn)化的字符變成一個或多個近似的字符,//ignore 會忽略掉不能轉(zhuǎn)化的字符,而默認(rèn)效果是從第一個非法字符截斷。

returns the converted string or false on failure.

使用:

1. 發(fā)現(xiàn)iconv在轉(zhuǎn)換字符"-"到gb2312時會出錯,如果沒有ignore參數(shù),所有該字符后面的字符串都無法被保存。不管怎么樣,這個"-"都無法轉(zhuǎn)換成功,無法輸出。另外mb_convert_encoding沒有這個bug.

2. mb_convert_encoding 可以指定多種輸入編碼,它會根據(jù)內(nèi)容自動識別,但是執(zhí)行效率比iconv差太多;如:$str = mb_convert_encoding($str,"euc-jp","ascii,jis,euc-jp,sjis,utf- 8");“ascii,jis,euc-jp,sjis,utf-8”的順序不同效果也有差異

3. 一般情況下用 iconv,只有當(dāng)遇到無法確定原編碼是何種編碼,或者iconv轉(zhuǎn)化后無法正常顯示時才用mb_convert_encoding 函數(shù)

from_encoding is specified by character code name before conversion. it can be array or string - comma separated enumerated list. if it is not specified, the internal encoding will be used.

$str = mb_convert_encoding($str, "ucs-2le", "jis, eucjp-win, sjis-win"); 
$str = mb_convert_encoding($str, "euc-jp', "auto");


本文鏈接:

收藏隨意^^請保留教程地址.

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 AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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

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)

Use java's Character.isDefined() function to determine whether a character is a defined character Use java's Character.isDefined() function to determine whether a character is a defined character Jul 25, 2023 am 10:33 AM

Use Java's Character.isDefined() function to determine whether a character is a defined character. In Java programming, sometimes you need to determine whether a character is a defined character. For convenience, Java provides the isDefined() function of the Character class, which can help us quickly determine whether a character is a defined character. This article explains how to use this function and provides some code examples. Character class represents a single character in Java

Recommended essential functions for Chinese processing: Detailed explanation of PHP iconv function Recommended essential functions for Chinese processing: Detailed explanation of PHP iconv function Jun 27, 2023 pm 02:04 PM

In the process of text processing, it is a common requirement to convert strings in different encoding formats. The iconv (InternationalizationConversion) function provided in the PHP language can meet this need very conveniently. This article will introduce the use of iconv function in detail from the following aspects: Definition of iconv function and introduction to common parameters Example demonstration: Convert GBK encoded string to UTF-8 encoded string Example demonstration: Convert UTF

Java documentation interpretation: Detailed explanation of the isAlphabetic() method of the Character class Java documentation interpretation: Detailed explanation of the isAlphabetic() method of the Character class Nov 04, 2023 am 10:05 AM

Interpretation of Java documentation: Detailed explanation of the isAlphabetic() method of the Character class 1. Overview In the Java Character class, the isAlphabetic() method is used to determine whether a given character is an alphabetic character. It returns a boolean value, true indicating that the given character is an alphabetic character, false indicating that the given character is not an alphabetic character. This article will provide a detailed analysis of the use and principle of this method, and provide code examples to help readers better understand

Java uses the isLetterOrDigit() function of the Character class to determine whether a character is a letter or number Java uses the isLetterOrDigit() function of the Character class to determine whether a character is a letter or number Jul 24, 2023 pm 02:45 PM

Java uses the isLetterOrDigit() function of the Character class to determine whether a character is a letter or number. In Java programming, we often need to perform some operations and judgments on characters. One of the common needs is to determine whether a character is a letter or a number. Java provides the isLetterOrDigit() function of the Character class to help us implement this function. The Character class is a wrapper class used to operate and judge characters.

Introduction to iconv command under CentOS Introduction to iconv command under CentOS Dec 29, 2023 pm 07:52 PM

iconv-fencoding[-tencoding][inputfile]...[Function] Converts the contents of a given file from one encoding to another. [Description]-fencoding: Convert characters from encoding to encoding. -tencoding: Convert characters to encoding. -l: List the known set of encoded characters -ofile: Specify the output file -c: Ignore illegal characters in the output -s: Suppress warning messages, but not error messages --verbose: Display progress information -f and -t can The specified legal characters are listed in the command with the -l option. [Example]* List currently supported character encodings

Use the isUpperCase() method of the Character class in Java to determine whether a character is an uppercase letter Use the isUpperCase() method of the Character class in Java to determine whether a character is an uppercase letter Jul 26, 2023 pm 01:09 PM

Use the isUpperCase() method of the Character class in Java to determine whether a character is an uppercase letter. In the Java programming language, we often need to determine whether a character is an uppercase letter. In order to realize this function, Java provides the Character class, whose isUpperCase() method can determine whether a character is an uppercase letter. This article will introduce the use of this method and sample code. Use isUpperCase() of the Character class

Use java's Character.isLetterOrDigit() function to determine whether a character is a letter or number Use java's Character.isLetterOrDigit() function to determine whether a character is a letter or number Jul 25, 2023 pm 04:22 PM

Use Java's Character.isLetterOrDigit() function to determine whether a character is a letter or number. In Java, we often need to determine whether a character is a letter or number. In order to simplify this process, Java provides a built-in function Character.isLetterOrDigit(), which can help us quickly complete this judgment. The Character.isLetterOrDigit() function accepts a character as a parameter

Java documentation interpretation: Detailed explanation of the isLowerCase() method of the Character class Java documentation interpretation: Detailed explanation of the isLowerCase() method of the Character class Nov 03, 2023 am 10:36 AM

Java documentation interpretation: Detailed explanation of the isLowerCase() method of the Character class. The Character class in Java provides many methods to handle character operations. The isLowerCase() method is used to determine whether a character is a lowercase letter. The specific use and application scenarios of this method will be explained in detail in this article. 1. The function and usage of isLowerCase() method The isLowerCase() method of Character class

See all articles