wc_remove_non_displayable_chars()
Removes useless non-displayable and problematic Unicode characters from a string.
This function eliminates characters that can cause formatting issues, invisible text, or unexpected behavior in copy-pasted text. Specifically, it removes:
- Soft hyphen (U+00AD) – Invisible unless text is broken across lines.
- Zero-width spaces & joiners (U+200B–U+200D) – Invisible and can cause copy/paste issues.
- Directional markers (U+200E–U+200F, U+202A–U+202E) – Can affect text rendering.
- Byte Order Mark (BOM) (U+FEFF) – Can interfere with encoding.
- Interlinear annotation characters (U+FFF9–U+FFFB) – Rarely used and unnecessary in checkout fields.
It does not remove:
- Non-breaking space (U+00A0) – Useful for preventing line breaks in addresses.
- Word joiner (U+2060) – Sometimes needed for proper text rendering in certain scripts.
No Hooks.
Returns
String. The sanitized string without problematic characters.
Usage
wc_remove_non_displayable_chars( $raw_value ): string;
- $raw_value(string) (required)
- The input string to sanitize.
Changelog
| Since 9.9.0 | Introduced. |
wc_remove_non_displayable_chars() wc remove non displayable chars code WC 10.4.3
function wc_remove_non_displayable_chars( string $raw_value ): string {
$remove_chars = array(
"\u{00AD}", // Soft Hyphen.
"\u{200B}", // Zero Width Space.
"\u{200C}", // Zero Width Non-Joiner.
"\u{200D}", // Zero Width Joiner.
"\u{200E}", // Left-to-Right Mark.
"\u{200F}", // Right-to-Left Mark.
"\u{202A}", // Left-to-Right Embedding.
"\u{202B}", // Right-to-Left Embedding.
"\u{202C}", // Pop Directional Formatting.
"\u{202D}", // Left-to-Right Override.
"\u{202E}", // Right-to-Left Override.
"\u{FEFF}", // Byte Order Mark (BOM).
"\u{FFF9}", // Interlinear Annotation Anchor.
"\u{FFFA}", // Interlinear Annotation Separator.
"\u{FFFB}", // Interlinear Annotation Terminator.
);
return str_replace( $remove_chars, '', $raw_value );
}