PHP manage characters, not spaces?

Using POSIX Character Classes

How to match [: cntrl:] but excluding [: space:]?

$message = ereg_replace("[[:cntrl:]]", "", $message); 
+6
source share
2 answers
Functions

ereg_ * (POSIX) has been deprecated for a long time. You should not use these methods.

According to POSIX, Bracket Expressions [:cntrl:] allows the ASCII range [\x00-\x1F\x7F] (or unicode \p{Cc} ) and [:space:] allows [ \t\r\n\v\f] . Using asciitable.com to eliminate these characters, you will leave an exception list [\x20\x09-\x0D] . Performing the math, you are left with [\x00-\x08\x0E-\x1F\x7F] . and this leaves you with the following: PHP 5.3 and compatibility with enhancement, sanitation:

 $message = preg_replace('/[\x00-\x08\x0E-\x1F\x7F]+/', '', $message); 

Please note that VT (Vertical Tab) and FF (Feed Channel, New Page) are also saved. Depending on your situation, you can also remove them:

 $message = preg_replace('/[\x00-\x08\x0E-\x1F\x7F\x0A\x0C]+/', '', $message); 
+5
source

[[:cntrl:]] is basically [\x00-\x1f\x7F] , and [[:space:]] equivalent to [ \t\r\n\v\f] (ref) , so a long method would be to use [\x00-\x08\x0E-\x1F\7F] (space 0x20 and out of range cntrl, \t\r\n\v\f - from \ x09 to \ x0D)

0
source

Source: https://habr.com/ru/post/909875/


All Articles