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); ?>
实验效果
推荐学习:《PHP视频教程》
以上就是php 不能写汉字吗的详细内容,
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)