How do I remove accents from characters in a PHP string?

How do I remove accents from characters in a PHP string?

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

Stefan Bogdanescu

Founder & Senior Architect

How do I remove accents from characters in a PHP string?

You're trying to strip diacritics from a string like "Fóø Bår" before hashing it for a URL, and you've run into the classic iconv transliteration issue. Your code sets the locale, specifies UTF-8 as the source and ASCII with TRANSLIT as the target, yet the output is F?? B?r instead of the expected F'oo Bar.

This isn't a PHP logic error; it's an environment mismatch. The iconv extension in PHP can be built against two different libraries: glibc's native iconv or the GNU libiconv library. When your server uses the glibc version, transliteration often fails on complex Unicode characters, replacing them with question marks regardless of correct locale settings or encoding checks.

To verify which implementation you're using, check the phpinfo() output for the "iconv" section and look for a line like "Library: glibc iconv". If it says libiconv, your setup is fine; if it says glibc, you're dealing with its known limitations. You can also run php -r "echo iconv('UTF-8', 'ASCII//TRANSLIT', 'Fóø Bår');" directly from the command line to confirm behavior outside of scripts.

Why setlocale isn't enough

Many guides suggest setting setlocale(LC_ALL, "en_US.utf8") before calling iconv. This is correct in theory but ineffective if the underlying iconv library doesn't support robust transliteration for accented characters. Even if setlocale returns successfully and your locale appears in locale -a, the glibc implementation may still drop unsupported characters.

A reliable alternative: mb_convert_kana or custom normalization

If you cannot recompile PHP with libiconv (common on shared hosting or restricted environments), avoid relying on iconv for this task. Instead, use a combination of Unicode normalization and regex-based stripping. Here’s a practical approach:

$input = "Fóø Bår";

// Normalize to NFKD form (decomposes accented chars into base + combining marks)
$normalized = iconv('UTF-8', 'UTF-8', $input); // ensure UTF-8 if needed

if (function_exists('mb_convert_kana')) {
    $stripped = mb_convert_kana($input, 'K', 'UTF-8');
} else {
    // Fallback: use normalization + regex
    $normalized = iconv('UTF-8', 'UTF-8//IGNORE', $input); // basic sanity check
    $pattern = '/\p{M}/u'; // match combining diacritical marks (requires PCRE)
    $stripped = preg_replace($pattern, '', $input);
}

echo $stripped;

Note: The regex solution assumes PCRE with Unicode support (/u modifier), which is standard in modern PHP versions. If you're on an older version, use mb_convert_kana or implement a manual mapping table for common accented characters.

Using transliteration safely when iconv works

If your environment does support libiconv, this approach remains valid:

setlocale(LC_ALL, "en_US.utf8");
$output = iconv("utf-8", "ascii//TRANSLIT", $input);
echo $output; // Outputs: F'oo Bar

However, always validate the output. Transliteration can produce unexpected substitutions (e.g., é → e, but ñ might become n). For URL slugs, consider further sanitizing with preg_replace('/[^a-z0-9\-_]/i', '', $output) to ensure only safe characters remain.

Trade-offs and pitfalls

  • Locale dependency: Relying on locale names like "en_US.utf8" can break on servers where that locale isn’t installed, even if it’s listed in locale -a.
  • Iconv inconsistency: Behavior varies between glibc and libiconv. Always test transliteration with edge cases before deploying.
  • Performance: Regex-based stripping is generally faster than iconv for simple accent removal on short strings.
  • URL safety: Remember that removing accents alone doesn’t make a string URL-safe. You still need to handle spaces, special symbols, and case normalization.

If you're building this into a larger application using Laravel, consider leveraging its helper functions or custom middleware to encapsulate slug generation logic. While laravelcompany.com offers resources on best practices for Laravel development, the core challenge here is environmental rather than framework-specific—so focus first on ensuring your PHP build supports reliable Unicode handling.

Ultimately, if iconv fails due to glibc, shift to normalization-based stripping as a drop-in replacement. It’s more predictable, doesn’t depend on locale quirks, and works consistently across most modern PHP installations.

Our network