Using the POSIX character classes
How to match [:cntrl:] but excluding the [:space:]?
$message = ereg_replace("[[:cntrl:]]", "", $message);
ereg_* (POSIX) functions have been deprecated for a long time now. You should not contiue using these methods.
According to POSIX Bracket Expressions [:cntrl:]
resolves to the ASCII range [\x00-\x1F\x7F]
(or the unicode \p{Cc}
) and [:space:]
resolves to [ \t\r\n\v\f]
. Using asciitable.com to resolve those characters, you are left with an exclusion list of [\x20\x09-\x0D]
. "Doing the math" you are left with [\x00-\x08\x0E-\x1F\x7F]
. and that leaves you with the following, PHP 5.3 and upward compatible, sanitization:
$message = preg_replace('/[\x00-\x08\x0E-\x1F\x7F]+/', '', $message);
Note that VT
(Vertical Tab) and FF
(Form Feed, New page) are also preserved. Depending on your situation you might want to remove these, too:
$message = preg_replace('/[\x00-\x08\x0E-\x1F\x7F\x0A\x0C]+/', '', $message);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With