Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: HTML::Entities encode_entities for perlmonks [ ]

by space_monk (Chaplain)
on Jun 15, 2013 at 08:49 UTC ( #1039090=note: print w/replies, xml ) Need Help??


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

Update: Do two runs of encode entities to encode normal characters first time and your special characters the second time. This may be a little slower than doing one pass but means you don't have to specify exactly what else you need to encode.

my $string = '[I haz square brackets]'; my $unsafe_chars = '[]'; # first pass encodes default set my $pass1= encode_entities( $string ); # second pass encodes speshul chars my $encoded = encode_entities( $pass1, $unsafe_chars );
If you spot any bugs in my solutions, it's because I've deliberately left them in as an exercise for the reader! :-)

Replies are listed 'Best First'.
Re^2: HTML::Entities encode_entities for perlmonks [ ]
by Anonymous Monk on Jun 15, 2013 at 09:03 UTC

    Nice try

    #!/usr/bin/perl -- use strict; use warnings; use HTML::Entities ; my $string = '[< smarto >]'; my $unsafe_chars = '[]'; my $encoded = encode_entities( $string, $unsafe_chars ); print "$string\n$encoded\n"; __END__ [< smarto >] &#91;< smarto >&#93;

    The question is what to add encode_entities to also encode  [ ] -- I want what the default encode_entities does plus  [ ]

      Next time be a bit more precise with your question! :-)

      You could simply run a first pass of encode_entities with no params so it would encode the default values, and the second pass with any special characters you also need encoding. I've amended my original answer accordingly.

      If you spot any bugs in my solutions, it's because I've deliberately left them in as an exercise for the reader! :-)
      Why not just encode the other characters manually?
      use HTML::Entities ; my $string = '[< smarto >]'; my $unsafe_chars = '[]'; my $encoded = encode_entities($string) =~ s{([\Q$unsafe_chars\E])}{sprintf"&#%d;",ord$1}egr; print "$string\n$encoded\n";

        Why not just encode the other characters manually?

        :) Q: How do I fix this here knife? A: Why not use another knife?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1039090]
help
Chatterbox?
and the questions are moot...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2016-12-09 23:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (158 votes). Check out past polls.