做留言板的时候rtrim() 没有成功去除指定字符
代码如下:
$notes=file_get_contents('liuyan.txt');
$notes=rtrim($notes,'@');
echo $notes;
调试后发现从文本中取出的字符串是UTF-8编码格式,而编辑器中输出的英文字符串为ASCII格式(中文是UTF-8格式)
$notes=file_get_contents('liuyan.txt');
echo '从文本中取出的字符串编码格式为:'.mb_detect_encoding($notes).'<br>';
$str='aaa地方df';
echo '当前编辑器输出的字符串编码格式为:'.mb_detect_encoding($str).'<br>';
输出:
从文本中取出的字符串编码格式为:UTF-8
当前编辑器输出的字符串编码格式为:ASCII
然后尝试用mb_convert_encoding($str, "ASCII", "auto")函数转换字符的编码,结果这个函数无效,开始查找原因。
echo phpinfo( );
发现mbstring扩展已经开启
1、mb_detect_encoding — 检测字符的编码
说明
string
mb_detect_encoding (
string
$str
[,
mixed $encoding_list
= mb_detect_order() [,
bool $strict
= false ]] )
检测字符串 str
的编码。
2、mb_convert_encoding — 转换字符的编码
说明
string mb_convert_encoding ( string
将 string 类型 $str
, string $to_encoding
[, mixed $from_encoding
= mb_internal_encoding() ] )
str
的字符编码从可选的 from_encoding
转换到 to_encoding
。