有特殊字符PHP麻烦通过读取txt文件flie_get_contents
问题描述:
我有这样的代码:有特殊字符PHP麻烦通过读取txt文件flie_get_contents
<?php
$s2 = file_get_contents("s2.txt");
?>
<div id="slogan">
<em>asd</em>
<strong><?php echo $s2; ?></strong>
<span>dsa</span>
</div>
例如,如果在我已写入的文件(s2.txt):ACS;它会显示我a s。 我能做些什么来写açs而不是a ?s?
答
如果您将$ s2字符串编码为UTF-8,它应该可以工作。
你可以做到这一点与PHP函数utf8_encode(string)
<?php
$s2 = file_get_contents("s2.txt");
?>
<div id="slogan">
<em>asd</em>
<strong><?php echo utf8_encode($s2); ?></strong>
<span>dsa</span>
</div>
这应该可以解决它。
答
你可以试试下面的代码片段:
function file_get_contents_utf8($fn) {
$content = file_get_contents($fn);
return mb_convert_encoding($content, 'UTF-8',
mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true));
}
$s2 = file_get_contents_utf8("s2.txt");
这串编码为UTF8
您也可以编码的file_get_contents()来代替,这是否为你更好。 – MadsBinger