Home > Backend Development > PHP Tutorial > PHP遍历中文目录的gbk编码和utf-8编码之间如何转换

PHP遍历中文目录的gbk编码和utf-8编码之间如何转换

WBOY
Release: 2016-06-23 13:30:32
Original
961 people have browsed it

我原本文件编码和网页输出编码都是 utf-8 的,
但是遍历中文目录,一定要将编码转成gbk编码,结果得到的数据也是gbk编码的。


--------------------------------------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------------------------------------

但是我还是想用 utf-8编码输出网页,那就需要把 gbk编码转成 utf-8编码,我试了两个方法都不行。


所以这个我要怎么才能把 $pdf 的数据由 gbk 转成 utf-8 呢?
使得 print_r($pdf) 在 情况下网页不乱码。


回复讨论(解决方案)

foreach($pdf as $k => $v){    $pdf[$k]=iconv('gbk','utf-8',$v);}
Copy after login

问题在于你的两个写法都是残废的

foreach(glob($dir . $ext) as $v) $pdf[] = iconv('gbk', 'utf-8', $v);
Copy after login

foreach($pdf as $v){
    $v = iconv('gbk', 'utf-8', $v);
}

你这样只是修改了$v,$pdf并没有修改。

可以该成这样.

foreach( &$pdf as $v){
    $v = iconv('gbk', 'utf-8', $v);
}

3楼说的对
后来我也发现了这个问题了。
我改写成
foreach($pdf as &$v){
  ............
}
就样行了。

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