php字符串编码转换 iconv与mb_convert_encoding的区别
PHP判断字符串编码函数mb_detect_encoding总结
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)
iconv — 字符串按要求的字符编码来转换
mb_convert_encoding — 转换字符的编码
这两个函数功能类似都是用来转换字符串编码的;
用法:
string mb_convert_encoding ( string str, string to_encoding [, mixed from_encoding] )
注:需要先启用 mbstring 扩展库,在 php.ini里将; extension=php_mbstring.dll 前面的 ; 去掉
参数:str——要编码的str、to_encoding——str要转换成编码类型、from_encoding——在转换前通过字符代码名称来指定。 它可以是一个 array 也可以是逗号分隔的枚举列表。 如果没有提供 from_encoding,则会使用内部(internal)编码。 参见支持的编码。
支持的字符编码
当前 mbstring 模块支持以下的字符编码。这些字符编码中的任意一个都能指定到 mbstring 函数中的 encoding 参数。
该 PHP 扩展支持的字符编码有以下几种:
UCS-4*
UCS-4BE
UCS-4LE*
UCS-2
UCS-2BE
UCS-2LE
UTF-32*
UTF-32BE*
UTF-32LE*
UTF-16*
UTF-16BE*
UTF-16LE*
UTF-7
UTF7-IMAP
UTF-8*
ASCII*
EUC-JP*
SJIS*
eucJP-win*
SJIS-win*
ISO-2022-JP
ISO-2022-JP-MS
CP932
CP51932
SJIS-mac** (别名: MacJapanese)
SJIS-Mobile#DOCOMO** (别名: SJIS-DOCOMO)
SJIS-Mobile#KDDI** (别名: SJIS-KDDI)
SJIS-Mobile#SOFTBANK** (别名: SJIS-SOFTBANK)
UTF-8-Mobile#DOCOMO** (别名: UTF-8-DOCOMO)
UTF-8-Mobile#KDDI-A**
UTF-8-Mobile#KDDI-B** (别名: UTF-8-KDDI)
UTF-8-Mobile#SOFTBANK** (别名: UTF-8-SOFTBANK)
ISO-2022-JP-MOBILE#KDDI** (别名: ISO-2022-JP-KDDI)
JIS
JIS-ms
CP50220
CP50220raw
CP50221
CP50222
ISO-8859-1*
ISO-8859-2*
ISO-8859-3*
ISO-8859-4*
ISO-8859-5*
ISO-8859-6*
ISO-8859-7*
ISO-8859-8*
ISO-8859-9*
ISO-8859-10*
ISO-8859-13*
ISO-8859-14*
ISO-8859-15*
byte2be
byte2le
byte4be
byte4le
BASE64
HTML-ENTITIES
7bit
8bit
EUC-CN*
CP936
GB18030**
HZ
EUC-TW*
CP950
BIG-5*
EUC-KR*
UHC (CP949)
ISO-2022-KR
Windows-1251 (CP1251)
Windows-1252 (CP1252)
CP866 (IBM866)
KOI8-R*
* 表示该编码也可以在正则表达式中使用。
** 表示该编码自 PHP 5.4.0 始可用。
任何接受编码名称的 php.ini 条目同样也可以使用 "auto" 和 "pass" 的值。 接受编码名的 mbstring 函数同样也可以使用值 "auto"。
如果设置了 "pass",将不会对字符的编码进行转化。
如果设置了 "auto",它将扩展成 NLS 中定义的每个字符编码列表。 比如,假设 NLS 设置为 Japanese,值将会认为是 "ASCII,JIS,UTF-8,EUC-JP,SJIS"。
NLS:国家语言支持(National Language Support)
string iconv ( string in_charset, string out_charset, string str )
注意:
第二个参数,除了可以指定要转化到的编码以外,还可以增加两个后缀://TRANSLIT 和 //IGNORE,
其中:
//TRANSLIT 会自动将不能直接转化的字符变成一个或多个近似的字符,
//IGNORE 会忽略掉不能转化的字符,而默认效果是从第一个非法字符截断。
Returns the converted string or FALSE on failure. (返回转换后的字符串;如果执行失败将返回FALSE。)
使用:
1. 发现iconv在转换字符 "-" 到gb2312时会出错,如果没有ignore参数,所有该字符后面的字符串都无法被保存。不管怎么样,这个 "-" 都无法转换成功,无法输出。 另外mb_convert_encoding没有这个bug。
2. mb_convert_encoding 可以指定多种输入编码,它会根据内容自动识别, 但是执行效率比iconv差太多;如:
$str = mb_convert_encoding($str,"euc-jp","ASCII,JIS,EUC-JP,SJIS,UTF-8");“ASCII,JIS,EUC-JP,SJIS,UTF-8”的顺序不同效果也有差异 。
3. 一般情况下用 iconv,只有当遇到无法确定原编码是何种编码,或者iconv转化后无法正常显示时才用mb_convert_encoding 函数 。
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");
例子:
$content = iconv("GBK", "UTF-8", $content);
$content = mb_convert_encoding($content, "UTF-8", "GBK");
/* 转换内部编码为 SJIS */ $str = mb_convert_encoding($str, "SJIS"); /* 将 EUC-JP 转换成 UTF-7 */ $str = mb_convert_encoding($str, "UTF-7", "EUC-JP"); /* 从 JIS, eucjp-win, sjis-win 中自动检测编码,并转换 str 到 UCS-2LE */ $str = mb_convert_encoding($str, "UCS-2LE", "JIS, eucjp-win, sjis-win"); /* "auto" 扩展成 "ASCII,JIS,UTF-8,EUC-JP,SJIS" */ $str = mb_convert_encoding($str, "EUC-JP", "auto");
$text = "This is the Euro symbol '€'."; echo 'Original : ', $text, PHP_EOL; echo 'TRANSLIT : ', iconv("UTF-8", "ISO-8859-1//TRANSLIT", $text), PHP_EOL; echo 'IGNORE : ', iconv("UTF-8", "ISO-8859-1//IGNORE", $text), PHP_EOL; echo 'Plain : ', iconv("UTF-8", "ISO-8859-1", $text), PHP_EOL; 输出结果: 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 This is the Euro symbol '

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In PHP, you can use the ord() function to convert characters into ascii code. This function can return the ASCII value of a single character or the first character in a string. The returned ASCII value will be displayed in integer form; the conversion syntax "ord (string)", the parameter "string" cannot be omitted, it is the string (or single character) from which the ASCII value is to be obtained.

There are two ways to replace a certain character with a null character in a PHP string: 1. Use the str_replace() function to replace the specified character with a null character. You only need to set the first parameter to the specified character and the second parameter to a null character. Syntax "str_replace("specified character","", $str)"; 2. Use the preg_replace() function with regular expressions to match the specified character and replace it with the null character, syntax "preg_replace('/specified character/', "",$str)".

Two removal methods: 1. Use preg_replace() to execute a regular expression to search for all uppercase letters and replace them with null characters. The syntax is "preg_replace('/[A-Z]/','',$str)". 2. Use preg_filter() to execute a regular expression to search for all uppercase letters and replace them with empty characters. The syntax is "preg_filter('/[A-Z]/','',$str)".

PHP is a typed programming language that is often used to develop web applications. During web development, you may need to perform various operations on strings, such as removing specific characters from a string, retaining numbers or letters in a string, etc. In this article, we will focus on how to remove specific characters on the left or right side of a string in PHP.

Two methods: 1. Use preg_match_all() with regular filter strings, the syntax is "preg_match_all("/[\x{4e00}-\x{9fff}]+/u","$str",$arr);" ; 2. Use preg_replace() with regular search for non-Chinese letters in the string and replace them with empty characters. The syntax is "preg_replace("/[^\x{4E00}-\x{9FFF}]+/u" ,'',$str)".

PHP is a very popular programming language and one of the preferred tools for building dynamic websites. In PHP development, we often need to operate strings, and one common requirement is to remove double quotes from strings. In this article, we will introduce some methods to remove double quotes from PHP strings.

PHP can add characters to strings. Two implementation methods: 1. Use the string connector "." to splice the specified character to the beginning or end of the string. The syntax is "specified character. string" or "string. specified character"; 2. use substr_replace The () function can insert the specified character at the specified position of the string. The syntax is "substr_replace(string, specified character, specified position, 0)". The value at the specified position can be 0, negative or positive.

PHP is a commonly used server-side scripting language that is widely used in web development. PHP's processing of strings is quite flexible, making it easy to convert strings into date and time format. This article will introduce how to use PHP to convert a string into date and time format.
