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
Related videos on Youtube
Author by
LanceBaynes
Updated on September 18, 2022Comments
-
LanceBaynes over 1 year
Are there any scripts that can convert between text (e.g.
<hi>
) and the html entities version (<hi>
) like this website does? Or at least a PHP file?-
Alen Milakovic almost 13 yearsI'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 almost 13 yearsI 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 over 7 yearsWelcome 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.