Evify Posted August 25, 2012 Share Posted August 25, 2012 Hi everyone I'm having some trouble with some charsets when using get_file_content function. I made the followin PHP code: <?php $file = file_get_contents('omfirma.txt'); $unescape = array('?','?','?','?','?','?'); $escape = array('Æ','æ','Ø','ø','Å','å'); $content = str_replace($unescape, $escape, $file); echo $content; ?> But when i run it the special chars it was supposed to change to HTML entities are displayed as black squares with a ? inside of it. Can you please help me and show me where im doing it wrong? Quote Link to comment https://forums.phpfreaks.com/topic/267556-help-with-get_file_content/ Share on other sites More sharing options...
Christian F. Posted August 25, 2012 Share Posted August 25, 2012 This hasn't got anything to do with file_get_contents (), but decoding HTML entities. For that, you'll want to use htmlspecialchars_decode (). Also make sure that you've set the correct charset, and that all instances are using the same charset. I recommend using UTF-8, as it's the most compatible. Quote Link to comment https://forums.phpfreaks.com/topic/267556-help-with-get_file_content/#findComment-1372312 Share on other sites More sharing options...
Evify Posted August 25, 2012 Author Share Posted August 25, 2012 I tried this: $file = file_get_contents('omfirma.txt'); $content = htmlspecialchars_decode($file); echo $content; But it still doesn't work. My special chars are still shown as squares with questionmarks in them. Quote Link to comment https://forums.phpfreaks.com/topic/267556-help-with-get_file_content/#findComment-1372313 Share on other sites More sharing options...
Christian F. Posted August 25, 2012 Share Posted August 25, 2012 That hints towards trying to decode something as UTF-8, while it's encoded in a different charset. Exactly where this error occurs I cannot tell you, because I don't have enough information to determine that. So you'll need to go through every step along the way, and make sure that you're using the same charset every step of the way. If that isn't possible, then you'll need to convert to the correct one at the earliest possible time. For more information on how to do this, please see the PHP manual for Multi Byte string functions. Quote Link to comment https://forums.phpfreaks.com/topic/267556-help-with-get_file_content/#findComment-1372326 Share on other sites More sharing options...
Evify Posted August 25, 2012 Author Share Posted August 25, 2012 I solved the problem by saving the .txt file as UTF-8 instead of ANSI, and that did the trick. No PHP needed for it after all, but thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/267556-help-with-get_file_content/#findComment-1372340 Share on other sites More sharing options...
Christian F. Posted August 25, 2012 Share Posted August 25, 2012 Glad to hear that you got it sorted out, and thanks for letting us know what the problem was. PS: Now's a good time to mark the thread solved, just press the green button ↓← Quote Link to comment https://forums.phpfreaks.com/topic/267556-help-with-get_file_content/#findComment-1372348 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.