in reply to special characters in parsed json rendering badly in browser

Thank you all for your suggestions but I don't feel I'm any closer with this problem, partly my own fault for not being clear what I want. (Which i'm figuring out as I puzzle it out.) I'd like the text/data to be portable to other applications that can read HTML.

I did figure out (thanks poj) that I can set the encoding in the HTML page to have it display correctly in the browser. But if that text gets posted to another page or app I don't necessarily have control over its page encoding.

SO what I really want is to HTML encode those characters. But when I try HTML::Encode: for, say, the smart apostrophe:

use HTML::Entities; my($text) = "Kren’s 89th birthday"; print encode_entities($text), $/;

that one character gets converted to three HTML entities:

Kren’s 89th birthday

which displays a lot of garbage in the browser.

So I'm lost as to what's going on or how to resolve it. Perl is rendering the JSON string as a smart quote but HTML::Encode is improperly encoding it.

So far my best solution seems to be:

$event{'desc'} =~ s/’/\&\#39\;/g; $event{'desc'} =~ s/–/-/g; $event{'desc'} =~ s/—/ - /g; $event{'desc'} =~ s/‘/'/g; $event{'desc'} =~ s/'/'/g; $event{'desc'} =~ s/“/"/g; $event{'desc'} =~ s/”/"/g;

but of course that only handles characters I'm aware of.

Thoughts? Thanks for your patience.

Scott

Replies are listed 'Best First'.
Re^2: special characters in parsed json rendering badly in browser
by poj (Abbot) on Sep 09, 2018 at 15:02 UTC

    see utf8 - The use utf8 pragma tells the Perl parser to allow UTF-8 in the program text in the current lexical scope.

    use utf8; use HTML::Entities; my($text) = "Kren’s 89th birthday"; print encode_entities($text), $/; # result right single quote # Kren’s 89th birthday
    poj

      That's it! That's all I needed, thank you!