更新时间:2023-02-23 09:25:27
通常 ANSI 代码是由 ESC (\033
aka \x1b
) 引入的,一个空心方块括号,然后是数字(可能重复:*[32;40m
)并以字母结尾.
Usually ANSI codes are introduced by an ESC (\033
aka \x1b
), an open square bracket, then numbers (possibly repeated: *[32;40m
) and terminated by a letter.
你可以使用类似 #\\x1b[[][0-9]+(;[0-9]*)[A-Za-z]#
到 preg_replace
他们都被遗忘了.
You can use something like #\\x1b[[][0-9]+(;[0-9]*)[A-Za-z]#
to preg_replace
them all to oblivion.
这有效(刚刚测试),即使绝对是矫枉过正:
This works (just tested), even if definitely overkill:
$test = preg_replace('#\\x1b[[][^A-Za-z]*[A-Za-z]#', '', $test);
I've also found this on GitHub, and this on SO.