PHP在txt文件中恢复html特殊字符

时间:2013-12-24 20:19:51

标签: php replace html-entities htmlspecialchars

我目前有一个如下所示的txt文件:

ABCDEF
Blah’s Test
12344
Blah’s Test
Testing

我尝试将特殊字符转换为实际字符,例如,我尝试将原始的txt文件转换为:

ABCDEF
Blah's Test
12344
Blah's Test
Testing

要做到这一点,我使用html_entity_decode()但是,我得到的结果不是我的预期结果:

ABCDEF
Blah’s Test
12344
Blah’s Test
Testing

如何修复我的代码以执行我想要的操作?

我的代码:

<?php
$items = file_get_contents('test1.txt');
$items = html_entity_decode($items);
file_put_contents("test2.txt", $items);
?>

3 个答案:

答案 0 :(得分:2)

这是字符集

html_entity_decode($a, ENT_QUOTES, 'cp1251');

答案 1 :(得分:1)

使用字符编码选项。

html_entity_decode($string, ENT_COMPAT, 'UTF-8');

答案 2 :(得分:0)

您使用的是哪个版本的PHP?

在5.4.0之前的版本中html_entity_decode()函数默认为ISO-8859-1编码。在5.4.0及更高版本中,它默认为UTF-8编码。我怀疑你使用的是早于5.4.0的版本。因此,您正在使用的UTF-8字符未被正确解码。

尝试将UTF-8作为第二个参数传递。有关详细信息,请参阅此处:http://www.php.net/html_entity_decode