It's the character set
html_entity_decode($a, ENT_QUOTES, 'cp1251');
Frage
I currently have a txt file that looks like this:
ABCDEF
Blah’s Test
12344
Blah’s Test
Testing
I tried to convert the special characters into their actual characters, so for example, I attempted make the original txt file into this:
ABCDEF
Blah's Test
12344
Blah's Test
Testing
To do this I used html_entity_decode()
however, instead of my expected results, I'm getting something like:
ABCDEF
Blah’s Test
12344
Blah’s Test
Testing
How can I fix my code to do what I want?
My code:
<?php
$items = file_get_contents('test1.txt');
$items = html_entity_decode($items);
file_put_contents("test2.txt", $items);
?>
Lösung
It's the character set
html_entity_decode($a, ENT_QUOTES, 'cp1251');
Andere Tipps
Use the character encoding option.
html_entity_decode($string, ENT_COMPAT, 'UTF-8');
What version of PHP are you using?
In versions prior to 5.4.0 The html_entity_decode() function defualts to ISO-8859-1 encoding. In version 5.4.0 and later it defaults to UTF-8 encoding. I suspect you are using an earlier version than 5.4.0. As a result, the UTF-8 characters you're using are not being properly decoded.
Try passing UTF-8 as a second parameter. See here for more info: http://www.php.net/html_entity_decode