wc_remove_non_displayable_chars()
Removes useless non-displayable and problematic Unicode characters from a string.
Covers the characters that are invisible to the reader but still count as content to anything matching on the string: soft hyphen, zero-width spaces and joiners, bidirectional marks, isolates and overrides, variation selectors, Hangul and Mongolian fillers, tag characters, and the byte order mark. Interlinear annotation marks (U+FFF9–U+FFFB) are removed too, though they are not invisible.
Copy-pasting from PDFs, word processors and web pages routinely carries these along, and a reader has no way to see why the value they pasted is being rejected.
Non-breaking spaces (U+00A0, U+202F) are not removed. They are visible spaces, so deleting them would run words together rather than restore the intended text.
Хуков нет.
Возвращает
string. The sanitized string without problematic characters.
Использование
wc_remove_non_displayable_chars( $raw_value ): string;
- $raw_value(строка) (обязательный)
- The input string to sanitize.
Список изменений
| С версии 9.9.0 | Введена. |
| С версии 11.2.0 | Removes every Unicode default-ignorable code point, including the word joiner (U+2060) and the variation selectors earlier versions kept. |
Код wc_remove_non_displayable_chars() wc remove non displayable chars WC 11.3.0
function wc_remove_non_displayable_chars( string $raw_value ): string {
// Ranges rather than \p{Default_Ignorable_Code_Point}: that property needs PCRE2 10.43,
// newer than the build many supported installs link against. An unsupported property
// makes preg_replace return null instead of failing loudly.
$pattern = '/['
. '\x{00AD}\x{034F}\x{061C}\x{115F}\x{1160}\x{17B4}\x{17B5}\x{180B}-\x{180F}'
. '\x{200B}-\x{200F}\x{202A}-\x{202E}\x{2060}-\x{206F}\x{3164}'
. '\x{FE00}-\x{FE0F}\x{FEFF}\x{FFA0}\x{FFF0}-\x{FFFB}'
. '\x{1BCA0}-\x{1BCA3}\x{1D173}-\x{1D17A}\x{E0000}-\x{E0FFF}'
. ']/u';
$result = preg_replace( $pattern, '', $raw_value );
if ( null !== $result ) {
return $result;
}
// Malformed UTF-8 makes a /u pattern bail. Fall back to a byte-wise replacement over the
// soft hyphen, zero-width characters, bidirectional marks, BOM and interlinear annotation
// marks, so such input is cleaned no less than it was before this pattern existed.
$fallback_chars = array(
"\u{00AD}",
"\u{200B}",
"\u{200C}",
"\u{200D}",
"\u{200E}",
"\u{200F}",
"\u{202A}",
"\u{202B}",
"\u{202C}",
"\u{202D}",
"\u{202E}",
"\u{FEFF}",
"\u{FFF9}",
"\u{FFFA}",
"\u{FFFB}",
);
return str_replace( $fallback_chars, '', $raw_value );
}