I've been having some trouble with a certain web API from a certain large online marketplace site... They are returning multiple results that seem to be the same just different iterations of applying utf8 encoding to the same string. Ex:
Some Companyî Some CompanyÃÂÃÂÃÂÃÂÃÂÃÂÃÂî Some CompanyÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂî
I'm pretty sure the intention in this example is the ® symbol - a registered trademark type symbol. Somehow on the companies end this data is getting screwed up, and they are apparently working on it - though that might mean a few months of no fix, and I have historical data that I need to use in the mean time. What I'm looking for is an equivalent way to do what the "utf8_decode" function in PHP does, but in Perl. A simple example in PHP would be something like this, that does what I want:
$prev = $value; for($i=0;$i<999;$i++) { $value = utf8_decode($value); if ($prev == $value) { break; } $prev = $value; print "[$i]$value\n"; }
I've been trying to get Encode::encode and Encode::decode to work here, but they don't seem to be able to actually unravel this mess. With my PHP hack script I can decode it to this:
Some Companyî [0]Some Company® [1]Some Company® ------------------ Some CompanyÃÂÃÂÃÂÃÂÃÂÃÂÃÂî [0]Some CompanyÃÂÃÂÃÂî [1]Some CompanyÃÂî [2]Some Companyî [3]Some Company® [4]Some Company® ------------------ Some CompanyÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂÃÂî [0]Some CompanyÃÂÃÂÃÂÃÂÃÂÃÂÃÂî [1]Some CompanyÃÂÃÂÃÂî [2]Some CompanyÃÂî [3]Some Companyî [4]Some Company® [5]Some Company®
I would probably then want to re-encode the final one back into utf8 and proceed from there. I know this is very hacky, but I need a workaround until they fix the data source. Any ideas?

In reply to doulbe/triple/multi encoded utf8 by fert

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.