0

While processing the input/output of a process created with proc_open, I've been hit with the special terminal ANSI codes (\033[0J,\033[13G), aside from not finding a reference to what these particular codes are doing, they are really messing with my preg_match calls.

Does PHP have a built in method for cleansing these types of strings? Or what would be the correct expression to use with preg_replace? Please note, I am dealing with non ascii characters, so stripping everything except... will not work.

Twifty
  • 3,267
  • 1
  • 29
  • 54

1 Answers1

6

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.

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.

Community
  • 1
  • 1
LSerni
  • 55,617
  • 10
  • 65
  • 107
  • I can't get that escape character to match, I tried changing it to the [\u001b] but I get an error "Compilation failed: PCRE does not support \L, \l, \N{name}, \U, or \u at offset 2". – Twifty Nov 21 '16 at 23:38
  • `\u` is Javascript. In PHP you want `\x1b`, and since single quotes will still escape \, you want `\\x1b`. See updated answer. – LSerni Nov 21 '16 at 23:44