How to convert to HTML code?

14,557

Solution 1

The perl CGI module has a escapeHTML function that makes it pretty easy:

perl -e 'use CGI qw(escapeHTML); print escapeHTML("<hi>\n");'

Or to do an entire file:

perl -p -e 'BEGIN { use CGI qw(escapeHTML); } $_ = escapeHTML($_);'  FILENAME

Solution 2

The recode utility supports HTML as one of the encodings. (You can even specify an HTML version.) In the text-to-entities direction, it will also recode non-ASCII characters into entities; you need to specify the correct input encoding (e.g. ASCII, latin1, utf-8, …).

recode utf8:html <input-file.txt >output-file.txt
recode l1..html file-to-recode.txt

Solution 3

xmlstarlet can do it both ways:

echo '<em>Ampersands & angle brackets need to be encoded.</em>' |
xmlstarlet esc | 
xmlstarlet unesc
Share:
14,557

Related videos on Youtube

LanceBaynes
Author by

LanceBaynes

Updated on September 18, 2022

Comments

  • LanceBaynes
    LanceBaynes over 1 year

    Are there any scripts that can convert between text (e.g. <hi>) and the html entities version (&lt;hi&gt;) like this website does? Or at least a PHP file?

    • Alen Milakovic
      Alen Milakovic almost 13 years
      I'm not sure what "html entities version" means. Can you elaborate on how this differs from regular html conversioni? If you just want a text to html coverter, a quick search shows txt2html.sourceforge.net.
  • Michael Mrozek
    Michael Mrozek almost 13 years
    I don't think this is what he wants; he wants either a command-line tool or a PHP script that will take text input and escape HTML entities
  • Toby Speight
    Toby Speight over 7 years
    Welcome to Unix & Linux! Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.