Remove non-ASCII characters from string

Remove non-ASCII characters from string

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

Stefan Bogdanescu

Founder & Senior Architect

Remove non-ASCII characters from string

You are looking at a string that visually renders as  because your application is interpreting bytes incorrectly. When you see these strange symbols, it usually means you have a mismatch between your source encoding and your target encoding. The byte sequence for the character  (U+00C2) in UTF-8 often represents the Latin Capital Letter A with circumflex. If that character appears where a simple ASCII newline or space should be, it is a classic sign of a double-encoding issue or an incorrect charset declaration.

To remove anything that isn't a standard ASCII character (bytes 0x00 through 0x7F), you need to be precise about what constitutes "non-ASCII." In the context of PHP and most modern web stacks, this means filtering out any byte with the high bit set (the 8th bit).

The Naive Byte-Level Approach

The most robust way to strip non-ASCII characters is to operate directly on the raw bytes rather than treating the string as a sequence of Unicode code points. If you rely solely on mb_convert_encoding or regex patterns like [^\\x00-\\x7F], you run into edge cases depending on how your source data is encoded.

If your input is UTF-8, you can iterate over each byte and keep it only if the value is less than 128. Here is a PHP implementation that handles this by forcing the string into a binary-safe context:

function removeNonAscii(string $text): string {
    // Ensure we are working with raw bytes to avoid surrogate issues in some encodings
    return preg_replace('/[^\x00-\x7F]/u', '', $text);
}

This pattern [^\\x00-\\x7F] matches any character outside the standard 7-bit ASCII range. The u modifier tells PCRE to treat the string as UTF-8, ensuring that multi-byte sequences are handled correctly before the replacement occurs. If you apply this regex to a string containing Â, it will be removed because that character requires bytes outside the 0x00-0x7F range.

Handling Double-Encoding Scenarios

The specific symptom of seeing  often points to UTF-8 bytes being interpreted as ISO-8859-1 (Latin-1). If a server receives UTF-8 data but the response header claims it is Latin-1, or if you decode a string that was already decoded once and then encoded again, you get these artifacts.

Simply removing non-ASCII characters might not be enough if your downstream logic expects specific control characters to remain intact (like newlines). If  appeared where a newline should be, converting the string back to UTF-8 first using mb_convert_encoding($text, 'UTF-8', 'ISO-8859-1') might restore the intended structure before you decide whether to strip the characters. However, if your goal is purely sanitization for storage or display in a context that strictly supports ASCII (like an email subject line or a legacy database field), the regex approach above is sufficient.

Trade-offs and Performance Considerations

While removing non-ASCII data simplifies encoding headaches, it introduces semantic loss. If you are processing content intended for international audiences, stripping everything after 127 destroys language support entirely. This approach is best suited for:

  • Legacy system migration where the target database only supports CHAR(1) or similar ASCII-only fields.
  • Data sanitization before sending to systems that cannot handle UTF-8 (e.g., certain older APIs).
  • Creating "safe" identifiers like slugs or email addresses where special characters are strictly forbidden.

Be aware that standard string functions in PHP might behave differently depending on the internal encoding state (mb_internal_encoding). Always ensure your error reporting is set to E_ALL during development so that warnings about invalid multibyte sequences do not get swallowed. If you encounter a warning, it usually indicates that your regex or conversion function received malformed UTF-8, which requires fixing the source data rather than just masking the symptom with a character filter.

Alternative Libraries and Tools

For complex text processing tasks involving multiple languages, manual string manipulation can become unwieldy. You might consider using dedicated libraries like IntlChar in PHP 7.4+ or utilizing Laravel's built-in helpers if you are within that ecosystem. While the core logic remains the same—filtering by byte value—wrapping this in a helper service ensures consistency across your application.

If you find yourself needing to handle this frequently in a Laravel project, creating a trait or a model scope that applies this transformation before saving can prevent data corruption at scale. However, remember that cleaning data at the storage layer is often easier than cleaning it at the display layer, as fixing encoding issues in HTML output requires escaping and decoding logic that adds significant overhead.

Our network