How to convert String with “ (ISO-8859-1) characters to normal (UTF-8)characters?

How to convert String with “ (ISO-8859-1) characters to normal (UTF-8)characters?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Fixing Mismatched Encodings in Raw HTML Strings

The core issue isn't a failure of your conversion logic; it is a mismatch between how PHP interprets the incoming byte stream and the actual encoding of those bytes. When you encounter characters like “ instead of proper quotation marks (“) or apostrophes, your data is almost certainly encoded in ISO-8859-1 (often called "Latin-1" or "Windows-1252") but being treated as UTF-8.

The sequence “ is the classic symptom of a double-encoding error. The character â represents byte 0xC2 in Latin-1, and € represents 0x82. When these two bytes are interpreted as a UTF-8 sequence, they form a single character: €. However, if that result is then processed again or displayed incorrectly, you get the garbled text.

Your current PHP snippet attempts to force a conversion using utf8_encode(), followed by decoding steps. This approach is fragile and likely flawed because it assumes the intermediate state of your string is valid UTF-8 before applying utf8_decode(). If the source data is raw Latin-1, you must decode it directly into UTF-8 without an intermediate encoding step that might corrupt the byte stream.

The Correct Conversion Path

To fix this in a database context where the column charset is already utf8, you need to treat the raw string as Latin-1 and explicitly cast it to UTF-8. The standard PHP function for this is utf8_decode(). This function converts an ISO-8859-1 byte sequence into a valid multi-byte UTF-8 representation.

Here is the corrected logic:

$raw_string = '<li>Jain R.K. and Iyengar S.R.K., “Advanced Engineering Mathematicsâ€, Narosa Publications,</li>';

// The raw string is ISO-8859-1. We need to convert it to UTF-8.
// utf8_decode() expects ISO-8859-1 input and outputs UTF-8.
$clean_string = utf8_decode($raw_string);

echo $clean_string;

This single operation handles the transformation. The “ sequence will resolve into the correct Unicode quotation mark. There is no need for utf8_encode() followed by utf8_decode() because that cycle does not solve the underlying byte interpretation error and can introduce new corruption if the intermediate state is malformed.

Handling HTML Entities

Once the encoding is fixed, you may still have HTML entities like &lt; (which represents <). If your goal is to store plain text in a database column rather than an escaped HTML string, you should decode these entities as well. However, you must be careful about the order of operations.

If you run html_entity_decode() before ensuring the bytes are valid UTF-8, you might encounter issues if the entity itself was double-encoded. In this specific case, since the quotes were corrupted at the byte level, fixing the encoding first is mandatory. After running utf8_decode(), your string will look like this:

<li>Jain R.K. and Iyengar S.R.K., "Advanced Engineering Mathematics", Narosa Publications,</li>

Now that the special characters are valid UTF-8 bytes, you can safely run html_entity_decode() if you need to remove the angle brackets and convert them back to actual HTML tags:

$clean_string = html_entity_decode($clean_string, ENT_QUOTES | ENT_HTML5, 'UTF-8');

Database Storage Strategy

It is crucial to distinguish between storing raw HTML and storing plain text. If your application expects the database to return a string that can be directly echoed into a browser without further escaping, you should store the decoded version (with <li> tags intact) in the utf8 column.

If you intend to render this content inside an HTML document later, ensure your view layer handles the final escaping. Storing the fully decoded string (with quotes and angle brackets resolved) is generally safer because it prevents double-escaping issues when fetching records from different parts of your application.

For high-volume data migration or batch processing, consider using database-level tools to handle charset conversion if the volume exceeds what fits in memory. However, for row-by-row updates, the PHP utf8_decode() function is reliable and efficient. Always verify that your database connection layer (PDO, MySQLi, etc.) has the correct default character set configured; otherwise, even with corrected PHP strings, the driver might re-encode them upon insertion based on its own configuration.

Our network