PHP中文乱码 布满荆棘的人生 2022-05-15 06:13 282阅读 0赞 **中文乱码** 就这一句就好使 header("Content-type:text/html; charset=utf-8"); **$\_GET 变量** 包含中文的GET变量,如果在PHP中使用,必须要转换成GBK格式,否则无法使用 <?php header("Content-type:text/html; charset=utf-8"); $dirname = $_GET['dirname']; echo $dirname.'<br />'; //必须要转换成GBK $dirname = iconv('utf-8', 'GBK', $dirname); echo $dirname.'<br />'; ?> 浏览器显示效果 ![1][] **完整例子** 以遍历目录为例 <?php header("Content-type:text/html; charset=utf-8"); $dirname = $_GET['dirname']; $dirname = iconv('utf-8', 'GBK', $dirname); function read_all ($dir){ if(!is_dir($dir)) return false; $handle = opendir($dir); if($handle){ while(($fl = readdir($handle)) != false){ $temp = $dir.DIRECTORY_SEPARATOR.$fl; if(is_dir($temp) && $fl!='.' && $fl != '..'){ read_all($temp); }else{ if($fl!='.' && $fl != '..'){ $temp = iconv('GBK', 'utf-8', $temp); //再将GBK编码转换为utf-8编码 echo $temp.'<br>'; } } } } } read_all($dirname); ?> 实验效果 ![1][1 1] [1]: /images/20220515/36b95a5580754b5f8ccc72ef6c770387.png [1 1]: /images/20220515/acc4b531bde74b0686e240124bfb23af.png
还没有评论,来说两句吧...