Skip to content

Instantly share code, notes, and snippets.

@zaa
Created December 23, 2009 19:29
Show Gist options
  • Select an option

  • Save zaa/262737 to your computer and use it in GitHub Desktop.

Select an option

Save zaa/262737 to your computer and use it in GitHub Desktop.
% License: NewBSD.
-module(escape_html).
-author(zaa@ikato.com).
-export([escapeHTML/1, selftest/0]).
escapeHTML(Str) ->
escapeHTML(Str, []).
escapeHTML([], Acc) ->
lists:reverse(Acc);
escapeHTML([$&|Rest], Acc) ->
escapeHTML(Rest, ";pma&" ++ Acc);
escapeHTML([$<|Rest], Acc) ->
escapeHTML(Rest, ";tl&" ++ Acc);
escapeHTML([$>|Rest], Acc) ->
escapeHTML(Rest, ";tg&" ++ Acc);
escapeHTML([$"|Rest], Acc) ->
escapeHTML(Rest, ";touq&" ++ Acc);
escapeHTML([H|Rest], Acc) ->
escapeHTML(Rest, [H] ++ Acc).
selftest() ->
"test &amp; &quot;test&quot; &lt;b&gt;TEST&lt;/b&gt;" == ?MODULE:escapeHTML("test & \"test\" <b>TEST</b>").
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment