php 不能写汉字吗
- 建站笔记
- 2022-05-13
- 684
php能写汉字,但是需要设置编码,其设置方法是在PHP页面中添加“header("Content-type:text/html; charset=utf-8");”代码即可。
本文操作环境:windows7系统、PHP7.1版、DELL G3电脑
php 不能写汉字吗?
php能写汉字,但是需要设置编码。
中文乱码
就这一句就好使
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 />'; ?>
浏览器显示效果
完整例子
以遍历目录为例
<?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); ?>
实验效果
本文由白琉璃于2022-05-13发表在白琉璃源码网,如有侵权或疑问,请联系我们,谢谢。
本文链接:https://www.bailiuli.com/t/2659.html
发表评论