Greetings Monks,

I'm trying to figure out how to filter/blank out bad UTF8 chars.

This particular snippet works perfectly to prevent croakage on bad UTF8 (ie, to identify bad UTF8 input prior to further processing):
use Encode qw(is_utf8); # check if $str is UTF8 and contains bad UTF8. print "bad UTF8\n" if is_utf8($str) and not is_utf8($str, 1);
Then, I may want to salvage what I can from $str (ie, filter/remove the bad UTF8 chars) by running it through iconv (which I read somewhere *may* remove bad UTF8 chars):
iconv -c --from UTF-8 --to UTF-8
However, that involves a slow shell call, so I tried Text::Iconv:
use Text::Iconv; my $conv = Text::Iconv->new("utf8", "utf8"); $str = $conv->convert($str);
But that does not filter out bad UTF8 chars.

Is there a fast/efficient cpan module/way to strip out any bad UTF8 chars?

If not a selective filter, then as a last resort, is there a brute-force method of simply removing *all* UTF8 chars?

Thanks

In reply to Filtering out bad UTF8 chars by FreakyGreenLeaky

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.