Getting ’ instead of an apostrophe(') in PHP

Getting ’ instead of an apostrophe(') in PHP

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

Stefan Bogdanescu

Founder & Senior Architect

The Hidden Byte: Decoding '’' in PHP Strings

You are staring at a string in your debug log and seeing "It’s Getting the Best of Me". You need it to be "It's Getting the Best of Me". You've already tried converting the text to UTF-8, perhaps using mb_convert_encoding, but the characters remain stubbornly incorrect. The culprit is rarely a broken font or a corrupted database column; it is almost certainly a mismatched character encoding between your data source and your application environment.

The specific sequence ’ is a classic telltale sign of mojibake, usually resulting from UTF-8 data being interpreted as Latin-1 (ISO-8859-1) or Windows-1252 before being re-encoded into UTF-8. When you see that specific three-byte sequence, it means the right single quote (apostrophe) originally existed in a proper four-byte UTF-8 format. However, an external system interpreted those bytes as if they were single-byte characters, outputting â (U+00E2), ’ (U+2019), and s.

To fix this, you must identify the source of the data. In your specific case, the data originates from an external URL. If you are scraping that content directly into a PHP variable using cURL or file_get_contents, you are likely inheriting the encoding declared by the remote server's HTTP headers or its internal database structure.

Inspecting and Correcting the Source Stream

The most robust solution is to ensure the data is decoded immediately upon arrival, rather than cleaning it later. If the remote server sends content with a Content-Type header of text/html; charset=utf-8, but your local environment expects something else, you might be getting confused by how the browser or CLI tool renders the output versus how the raw bytes are stored.

If you cannot change the source URL's configuration, you must handle the encoding normalization in PHP. The most reliable method is to detect the actual byte order and normalize it. While mb_convert_encoding is often the first instinct, it requires knowing the correct "from" and "to" codes. If you are seeing ’, the data was likely UTF-8 that was read as ISO-8859-1 (or Windows-1252).

You can attempt to reverse this specific corruption by converting the string from Windows-1252 (or ISO-8859-1) back into UTF-8. This effectively tells PHP, "Treat these bytes as if they were single-byte Latin characters, then re-encode them properly as multi-byte UTF-8."

// Assume $rawData contains the string with '’'
$corrected = mb_convert_encoding($rawData, 'UTF-8', 'ISO-8859-1');

// Or if the source is specifically Windows-1252 common in web scraping:
$corrected = mb_convert_encoding($rawData, 'UTF-8', 'Windows-1252');

After running this conversion, the ’ sequence will be recognized as a corrupted representation of a UTF-8 character that was misread as Latin-1. The function will remap those bytes correctly into the standard four-byte UTF-8 sequence for the right single quote (U+2019). You can then verify the fix by checking the byte length or using mb_strlen with the UTF-8 encoding explicitly set.

Handling External Data Sources

When pulling data from external APIs like TVRage, you must respect the Content-Type headers they return. If the server sends UTF-8 but your local script is running in an environment that defaults to a different encoding (like ASCII or ISO-8859-1), mismatches occur immediately.

It is critical to check the Content-Type header of the response you receive. If the header claims UTF-8, but the data still looks like Latin-1 garbage after reading it, it implies the server itself has sent incorrectly encoded bytes, or the header is lying. In such cases, forcing a conversion back from ISO-8859-1 to UTF-8 is often the necessary workaround, as the byte patterns of common accented characters and punctuation in Windows-1252 overlap significantly with valid UTF-8 sequences when misinterpreted.

Preventing Future Issues

Once you have normalized the string, you should persist it in your application using UTF-8 consistently. If you store this data in a database, ensure your connection character set is explicitly set to utf8mb4. MySQL's standard utf8 alias only supports up to three bytes per character, which means you cannot store emojis or certain special quotes properly. Using utf8mb4 ensures that the four-byte sequence for ’ is stored without truncation.

In a Laravel application, this configuration is handled at the database connection level in your .env file:

DB_CONNECTION=pgsql # or mysql
DB_CHARSET=utf8mb4

For Laravel specifically, you can also ensure the response and request encodings are handled gracefully using middleware. However, if the issue lies strictly with an external scraper, the mb_convert_encoding step described above remains the primary defense against character set drift.

Why Simple Conversion Fails Sometimes

You might wonder why simply calling mb_convert_encoding($string, 'UTF-8', 'UTF-8') didn't work for you initially. This command is a no-op; it does not analyze the content to guess the source encoding. It assumes the input is already valid UTF-8. Since your input contains the byte sequence for â (0xE2), ’ (0x80), and s (0x99) interpreted incorrectly, it was technically valid Latin-1 bytes that looked like garbage when treated as UTF-8. You needed to tell PHP to interpret those bytes as Latin-1 first, then re-map them to the correct UTF-8 representation.

By explicitly specifying the source encoding (ISO-8859-1 or Windows-1252) in your conversion function, you provide the necessary context for the library to reconstruct the original apostrophe character. This approach is deterministic and does not rely on guessing algorithms that might fail on edge cases.

Our network