Ashley Sheridan​.co.uk

A Formatting Class For PHP Command Line Scripts

Just over a year ago when I moved into my flat, I found myself at a loose end in that time between moving in and the Internet connection being set up. Having exhausted all my films, and played my fill of Red Alert, I was in desperate need for something to do.

This PHP script is the culmination of that free time; a command line script that can turn basic HTML strings into something that can be displayed on the CLI of your choice (assuming it supports the formatting). I've tested this primarily on Bash terminals (on both Linux and OSX), but it should work on some others.

The class itself is a basic, non-namespaced class (of course if you need you can add one in) that is simple to use. The most simple use-case is:

require_once("phpcli.php"); $cli->message('<font color="#f00">red text</font> with <b>bold</b> bit, <font background="#080" color="#000">green <u>background</u> text</font> here'."\n", true);

Most of the HTML should look familiar, but one thing I wanted to support (because command lines supported it very well) was background colours in an easy syntax. To this end, I added the background attribute to the <font> tag, as it felt the neatest place to put it, unless I was going to implement an entire CSS parser.

Currently, the class supports bold, italic, and underlined text. As well as this, all colours specified, both for background and foreground, are converted to the nearest colour that is within the standard 16 classic CLI colours.

I've fixed any major bugs I could fine, but if you do find any while you're using it, post an issue back against https://github.com/AshleyJSheridan/phpcli