好吧,给定:
- 0x09 = 制表符
- 0x12 = 换行
- 0x15 = 回车
那么除了上面的(和仍然<32
)之外的任何东西都会看起来像:
/[\x00-\x08\x10\x11\x13\x14\x16-\x1F]/
而且我假设您的意思是排他匹配(最多但不包括 32 个),否则最后一个十六进制代码应该是\x20
.
$orig = "This is a sample document. It contains:\r\n"
. "\t* horizontal tabs,\r\n"
. "\t* line feeds, and\r\n"
. "\t* carriage returns\r\n"
. "\r\n"
. "These characters are not to be removed. However, other characters, such as:\r\n"
. "\r\n"
. "\t'\x06' (ACK),\r\n"
. "\t'\x07' (BEL),\r\n"
. "\t'\x1B' (ESC)\r\n"
. "\t(others)\r\n"
. "\r\n"
. "And other characters < ordinal 32 should be removed.";
$modif = preg_replace('/[\x00-\x08\x10\x11\x13\x14\x16-\x1F]/', '', $orig);
echo str_repeat('=', 50) . PHP_EOL;
echo (strlen($orig) == strlen($modif) ? "Failed" : "Success") . PHP_EOL;
echo str_repeat('=', 50) . PHP_EOL;
echo PHP_EOL;
echo $modif;
基于$modif
短于$orig
(3 个字符 [ \x06
, \x07
, \x1B
]),但保留了空白字符([ \x09
, \x12
, \x15
]),我想说这就是您所追求的。