Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
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! :-)


Comment on Re: HTML::Entities encode_entities for perlmonks [ ]
Download Code
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  [ ]

      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?

      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! :-)

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 web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2014-08-21 09:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (129 votes), past polls