Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: HTML::Entities encode_entities for perlmonks [ ]

by kcott (Chancellor)
on Jun 15, 2013 at 09:35 UTC ( #1039095=note: print w/replies, xml ) Need Help??


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');
  • use utf8; — because source code contains UTF-8, i.e. the character.
  • binmode(STDOUT => ':utf8'); — doesn't change any of the character entity references but, without it, print $string; gives <[Here's my "2?" worth]> (note the ? instead of ) now that use utf8; has been added.

-- 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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1039095]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2018-09-26 01:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Eventually, "covfefe" will come to mean:













    Results (205 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!