PHP: Dealing special characters with iconv

PHP: Dealing special characters with iconv

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

Stefan Bogdanescu

Founder & Senior Architect

PHP: Dealing special characters with iconv

The iconv function often feels like a black box. You feed it a string, specify two encodings, and expect magic to happen. But when dealing with multi-byte UTF-8 characters, you frequently run into runtime notices about illegal or incomplete characters instead of the clean output you need for database storage or URL generation.

Consider this common scenario:

$string = "Löic & René";
$output = iconv("UTF-8", "ISO-8859-1//TRANSLIT", $string); 

You might expect Loic & Rene, but instead, PHP throws a notice: Detected an illegal character in input string. If you try with just "Löic", you get a different error: Detected an incomplete multibyte character.

These errors stem from how the iconv extension validates input before attempting conversion. The core issue is that iconv expects the input string to be strictly valid UTF-8. If your source data contains invalid byte sequences or if the function fails to map a character, it halts execution and warns you.

Handling Invalid Characters with Options

To solve this, you need to manipulate the target encoding option string. The standard options are //TRANSLIT and //IGNORE.

  • //TRANSLIT: Attempts to approximate characters that cannot be represented in the target character set (e.g., converting ö to o). This is ideal for creating clean URLs or storing simplified data in a database. However, if it encounters a character it absolutely cannot approximate without corrupting the string structure, it may fail depending on the specific environment settings.
  • //IGNORE: Drops characters that cannot be mapped. This results in data loss but guarantees no errors are thrown.
  • //TRANSLIT//IGNORE: This combination is a powerful hybrid. It attempts transliteration first; if that fails for a specific character, it ignores it rather than throwing an exception. This is particularly useful when dealing with complex diacritics like the hamza or alef found in Arabic names (e.g., Ê¿ABBĀSĀBĀD). Without this flag, you get an empty string and a notice. With it, you get ABBSBD if transliteration fails completely, but often the combination yields the desired ABBASABAD by handling the transliteratable parts gracefully while skipping the rest.

The Locale Trap

Even with the correct options, iconv relies on the system's locale settings to determine how characters are mapped. If your server is running under a default POSIX locale (like C or en_US.UTF-8), certain mappings might not be available or might behave unexpectedly.

To ensure accurate transliteration, you must explicitly set the locale before calling iconv. For example, if you need to transform rené into rene, setting the locale to a region that supports the necessary character mappings is critical:

setlocale(LC_CTYPE, 'nl_BE.utf8'); // or your specific locale

$string = 'rené';
$output = iconv('UTF-8', 'ASCII//TRANSLIT', $string);
echo $output; // outputs rene

Without this step, iconv defaults to POSIX behavior, which can lead to incomplete mappings or errors even when the input is valid UTF-8.

Practical Implementation for Databases and URLs

When designing your data layer, you often face two distinct requirements: storing clean identifiers versus displaying rich content.

  1. URL Storage: To generate clean URLs like loic-and-rene, use //TRANSLIT. This removes diacritics while preserving the base character set, making the slug readable and SEO-friendly.
  2. Display Storage: If you need to store the original text in a database column for display purposes, do not use iconv at all. Simply pass the string through htmlentities($string, ENT_QUOTES) when rendering it in an HTML page. This handles the escaping required for safe HTML output without altering the underlying character encoding stored in the database.

By understanding the interplay between input validation, locale settings, and option flags like //IGNORE, you can prevent runtime notices and ensure your data transformation logic is robust across different server environments.

Our network