Skip to content
Advertisement

How to remove all formatting from the output of a console command written in PHP

I have some commands that format the output to the console.

Those commands are written using the Symfony Console component and formatted using its styles.

When run in the console, the formatting is good, but those commands are often also run from a queue system I wrote and so the logs are then shown on an HTML page.

The result is really hard to understand as it is filled with all the formatting “tag” that are useful on the console but are completely unuseful when the output is read on a web page.

Here an example of what I’m saying:

[32mStarting command[39m
[32m===========================[39m

[34m[>] Analyzing Entity [39m[32This is the entity[39m[34m.                                             [39m
[34m[>] Starting analyzing Entity [39m[32mThis is the entity[39m[34m.                                    [39m
[34m[>] Creating new Job for [39m[30;42This is the entity[39;49m[34m to analyze it in [39m[30;42m+1 week[39;49m[34m (Cause: The Entity is a clone of [39m[30;42Entity2[39;49m[34m).[39m
[34m[>] New Job for [39m[30;42mEntity[39;49m[34m created.                                         [39m
...

As you can see the output is almost non readable.

How can I remove all the formatting from it before saving it to the database?

Advertisement

Answer

You can disable ANSI colors with the --no-ansi option

You can also try ANSI to HTML5 Converter

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement