PHP检测当前字符编码并转码

WBOY
Release: 2016-06-23 13:42:54
Original
1450 people have browsed it

一、检测当前字符串编码并将编码改为utf-8

1 获取当前字符串的编码

$encode = mb_detect_encoding($str, array("ASCII",'UTF-8',"GB2312","GBK",'BIG5')); 

2 将字符编码改为utf-8

$str_encode = mb_convert_encoding($str, 'UTF-8', $encode);


二、函数详解

mb_detect_encoding ? 检测字符的编码

说明

string  mb_detect_encoding (  string $str [,  mixed $encoding_list = mb_detect_order() [,  bool $strict = false ]] )

检测字符串 str 的编码。

参数

str

待检查的字符串。

encoding_list

encoding_list 是一个字符编码列表。 编码顺序可以由数组或者逗号分隔的列表字符串指定。

如果省略了 encoding_list 将会使用 detect_order。

strict

strict 指定了是否严格地检测编码。 默认是 FALSE。

返回值

检测到的字符编码,或者无法检测指定字符串的编码时返回 FALSE。

mb_convert_encoding ? 转换字符的编码

说明

string  mb_convert_encoding (  string $str ,  string $to_encoding [,  mixed $from_encoding = mb_internal_encoding() ] )

将 string 类型 str 的字符编码从可选的 from_encoding 转换到 to_encoding。

参数

str

要编码的 string。

to_encoding

str 要转换成的编码类型。

from_encoding

在转换前通过字符代码名称来指定。它可以是一个 array 也可以是逗号分隔的枚举列表。 如果没有提供 from_encoding,则会使用内部(internal)编码。

参见支持的编码。

返回值

编码后的 string。

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!