30 October, 2020

Minify or Remove new Lines from a File

For a WordPress plugin I need a program, that will remove any new lines in html. Then you can use the code in a PHP variable. In practise this is what a minifier does. There are many online tools, but I'd prefer to do it myself.

In Bash - that is Linux or Mac terminals - you can do this with just one line of code. First save your markup in a file test.html via your most trusted editor. 

Then do this:

#  tr -d '\n' < test.html > test.min.html


Understand this code:

  • tr = translate characters
  • -d = delete
  • '\n' = new line
  • < test.htm = from the file test.html
  • > test.min.html = save the result to the file test.min.html

Done.


No comments: