php识别中文编码并自动转换为UTF
May 25, 2016 pm 04:40 PM下面整理了php识别中文编码并自动转换为UTF-8几个例子,这些例子非常的不错,希望对各位有帮助.
php自动识别编码,若里面有中文的话,将其转换为UTF-8就最好了,因为中文在Gbk编辑情况情况下,有可能会乱码,这个和客户端和服务端编码都有关系,为了避免乱码,我们可以使用下面的函数将其自动转换为UTF8国际标准编码,代码如下:
<?php function characet($data){ if( !emptyempty($data) ){ $fileType = mb_detect_encoding()($data , array('UTF-8','GBK','LATIN1','BIG5')) ; if( $fileType != 'UTF-8'){ $data = mb_convert_encoding($data ,'utf-8' , $fileType); } } return $data; } ?>
Copy after login
补充:文件编码转换,代码如下:
$temstr=file_get_contents($path); $encode = mb_detect_encoding($temstr,"ASCII,UTF-8,CP936,EUC-CN,BIG-5,EUC-TW"); $temstr=mb_convert_encoding($temstr, "CP936", $encode);
Copy after login
例子,代码如下:
<?php $f=fopen("test.txt", "wb"); $text=utf8_encode("a!"); //先用函数utf8_encode将所需写入的数据变成UTF编码格式。 $text="\\xEF\\xBB\\xBF".$text; //"\\xEF\\xBB\\xBF",这串字符不可缺少,生成的文件将成为UTF-8格式,否则依然是ANSI格式。 fputs($f, $text); //写入。 fclose($f); ?>
Copy after login
永久链接:
转载随意!带上文章地址吧。
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
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot tools Tags

Hot Article
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

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)
