Pasting syntax highlighted (colourized) PHP code into documents


When writing technical documents, as I often do, there is often a need to include colourized code samples. If you're working with PHP, this code can be produced quite easily from the command line.

php --run 'highlight_file("/path/to/myfile.php");' > /tmp/code.html

This will use PHP itself to colourize the output. See highlight_file for details.

You can then open the HTML file with your favourite Web browser, cut the code that's displayed and then paste it into your document. This should work with OpenOffice, LibreOffice, Google Drive / Google Docs, and Microsoft Word.


Add new comment