PHP iconv () function character encoding conversion problem to explain _php skills - PHP

Source: Hi learning network sensitive and eager Forum www.piaodoo.com welcome to learn from each other

In php iconv library to complete the conversion between various character sets, is indispensable php based programming library; but sometimes less iconv transcoding data for some reason some will. For example, in the conversion character "-" it is wrong to gb2312.

Together slowly look below at the use of this function.

The simplest application, the gb2312 replaced utf-8:

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

With $text=iconv("UTF-8","GB2312",$text)the process, if you encounter some special characters, such as: -. "" "", The English name of the character, etc., convert it broke. Text after these characters had not even continue the conversion.

For this problem, you can use the following code to achieve:

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

You read that right, it's that simple, do not use gb2312, and written GBK, on ​​it.

As an alternative, the second argument, plus //IGNORE, ignore the error, as follows:

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

Comparison of these two methods is not particularly, a first felt (GBK place GB2312) method is better.

php manual iconv () Description:

iconv

(PHP 4 >= 4.0.5, PHP 5)
iconv – Convert string to requested character encoding
Description
string iconv ( string in_charset, string out_charset, string str )
Performs a character set conversion on the string str from in_charset to out_charset. Returns the converted string or FALSE on failure.
If you append the string //TRANSLIT to out_charset transliteration is activated. This means that when a character can't be represented in the target charset, it can be approximated through one or several similarly looking characters. If you append the string //IGNORE, characters that cannot be represented in the target charset are silently discarded. Otherwise, str is cut from the first illegal character.

When using this function string code conversion, it is noted that if the conversion is gb2312 utf-8, the character string may appear truncated occur. At this point you can use the following solutions:

$str=iconv('utf-8',"gb2312//TRANSLIT",file_get_contents($filepath));

I.e., the second parameter is added in red portion, he said: If no characters in a source encoding to match the target encoding, the characters will choose a similar conversion. Here you can also use: // IGNORE this parameter, omit the character can not be converted.

ignore mean error when converting ignored, if not ignore parameter, all behind the character string can not be saved.

The default is not a function iconv php, and it is the default module installation. You need to install to use.

If windows2000 + php, php.ini file you can modify the pre-extension = php_iconv.dll ";" removed, at the same time you want to copy iconv.dll php your original installation files to your winnt / system32 lower ( If your point is that this dll directory). In the linux environment, with the installation of a static way, we want to add a --with-iconv when configure it, phpinfo see iconv items. (Linux7.3 + Apache4.06 + php4.3.2).

mb_convert_encoding and iconv Functions Introduction

mb_convert_encodingThis function is used to convert coded. The original program code has not understood the concept, but it seems a little hang of it. But generally do not exist in English coding problems, Chinese data will only have this problem. For example, when you write a program using Zend Studio or Editplus, gbk coding is used, if the data needs into the database, and the database encoding is utf8, then it is imperative data encoding conversion, or into the database will become garbled .

Make a GBK To UTF-8:

<?php 
header("content-Type: text/html; charset=Utf-8"); 
echo mb_convert_encoding("妳係我的友仔", "UTF-8", "GBK"); 
?>

Again a GB2312 To Big5:

<? PHP 
header ( "Content-Type: text / HTML; charset = big5"); 
echo mb_convert_encoding ( "You are my friend", "big5", "GB2312"); 
?>

However, to use the function above needs to be installed, but you need to enable mbstring extensions.

string mb_convert_encoding ( string str, string to_encoding [, mixed from_encoding] )Need to enable mbstring extensions, will in php.ini; extension = php_mbstring.dll the front; remove mb_convert_encoding can specify multiple input code, it automatically identified based on the content, but the efficiency ratio iconv minds;

string iconv ( string in_charset, string out_charset, string str )Note: The second parameter can specify in addition to the transformed coding, it also can be increased two suffixes: // // TRANSLIT and the IGNORE, wherein the character TRANSLIT // will automatically do not directly translate into one or more approximate character, // iGNORE ignore character off can not be converted, and the default effect is cut from the first illegal character.

Under normal circumstances with iconv, only when the original could not be identified coding is what encoding or conversion iconv does not display properly when using mb_convert_encoding the function.

$content = iconv("GBK", "UTF-8″, $content);
$content = mb_convert_encoding($content, "UTF-8″, "

to sum up

That's all for this article, I hope the contents of this paper has some reference value of learning for all of us to learn or work, thank you for the support sensitive and eager Forum / Hi learning network. If you want to know more details, please see the related links below

The original address is: http: //www.piaodoo.com/thread-3559-1-1.html

Guess you like

Origin www.cnblogs.com/txdah/p/12093186.html