Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: special characters in parsed json rendering badly in browser

by slugger415 (Monk)
on Sep 09, 2018 at 13:55 UTC ( #1221978=note: print w/replies, xml ) Need Help??


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!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2020-03-29 21:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    To "Disagree to disagree" means to:









    Results (171 votes). Check out past polls.

    Notices?