TmCrafz
TmCrafz

Reputation: 184

Display text in UTF-8 charset without wrong characters

I have a text file with some content in it, which I want to display on my webpage. I'm loading the content this way:

$txt = file_get_contents('new.txt');

When I display the content with charset=UTF-8 it looks like this:

enter image description here

When I use charset=ISO-8859-1 instead it looks like this:

enter image description here

I want that the Text is looking like the second example, but while using charset=UTF-8 instead of ISO-8859-1. How can I convert the text so it's displayed right?

Upvotes: 0

Views: 704

Answers (1)

Koala Yeung
Koala Yeung

Reputation: 7903

You can either work on the raw data or converting on the fly.

If you're planning to have all new data to stored in UTF-8 format, then batch converting all old data would be more favorable. It is not fun to have mixed encoding in your raw data. You may reference this question to find batch conversion command advice.

On the otherhand, if you're going to keep the input and storage in ISO-8859-1 encoding, the only thing you can do is to convert the document on the fly.

$txt = iconv('iso-8859-1', 'utf-8', file_get_contents('new.txt'));

Or if your source files have mixed encoding iso-8859-1 and other unknown encoding, you may add //IGNORE flag to prevent error:

$txt = iconv('iso-8859-1', 'utf-8//IGNORE', file_get_contents('new.txt'));

This takes more computation power to display the page every time. So it is always preferable to have the raw content converted (unless it is not possible for your situation).

Upvotes: 1

Related Questions