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


In reply to Re: special characters in parsed json rendering badly in browser by slugger415
in thread special characters in parsed json rendering badly in browser by slugger415

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.