Escaping HTML so you can quote it in HTML

26 Feb 2004 10:13

Quick tip: How to escape HTML text so that it can be quoted in another piece of HTML, e.g. in a <pre> tag.

cat index.html | \
    sed 's/&/\&amp;/' | \
    sed 's/"/\&quot;/' | \
    sed 's/</\&lt;/' | \
    sed 's/>/\&gt;/'

There’s probably a simpler way to do this, but it works for me, so I’m putting it here before I forget.