http://www.perlmonks.org?node_id=1039095


in reply to HTML::Entities encode_entities for perlmonks [ ]

Using the HTML::Entities documentation:

#!/usr/bin/env perl -l use strict; use warnings; use utf8; binmode(STDOUT => ':utf8'); use HTML::Entities; my $unsafe_chars = "<&>'\"[]\200-\377"; my $string = "<[Here's my \"2¢\" worth]>"; print $string; print encode_entities($string, $unsafe_chars);

Output:

$ pm_html_ent_plus_brackets.pl <[Here's my "2¢" worth]> &lt;&#91;Here&#39;s my &quot;2&cent;&quot; worth&#93;&gt;

Update: Oops! just noticed &Acirc; in the output (just before &cent;). Fixed by adding:

use utf8; binmode(STDOUT => ':utf8');

-- Ken

Replies are listed 'Best First'.
Re^2: HTML::Entities encode_entities for perlmonks [ ]
by Anonymous Monk on Jun 15, 2013 at 09:47 UTC
    Thanks, that looks almost complete, I think cntrl is missing I tried using [:cntrl:] but that didn't work -- OTOH using  my $unsafe_chars = q{\x00-\x1f<&>'"[]\200-\377}; worked

      Fair comment. I hadn't previously used this module so, as indicated, I was working from the doco: I missed the "control chars" part of:

      The default set of characters to encode are control chars, high-bit chars, and the <, &, >, ' and " characters.

      Also, note the update regarding the utf8 pragma.

      -- Ken