Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: Fixing utf8 errors

by Rodster001 (Pilgrim)
on Oct 31, 2015 at 16:07 UTC ( [id://1146582]=note: print w/replies, xml ) Need Help??


in reply to Re: Fixing utf8 errors
in thread Fixing utf8 errors

Here is an example:
print Dumper $text; $VAR1 = { 'string' => "Text\x{daed}", }; print $text->{'string'}; 'Unicode surrogate U+DAED is illegal in UTF-8 at line 5.

Replies are listed 'Best First'.
Re^3: Fixing utf8 errors
by graff (Chancellor) on Nov 01, 2015 at 01:26 UTC
    It's possible for UTF8-encoded text to contain code points in the range U+D800 - U+DFFF (Surrogate Pair Area), but this is always a sign that something bad was done to the data when it was converted into UTF8 encoding.

    I don't recall offhand what particular mistake(s) in transcoding would cause these code points to show up in UTF8 data, but there's a chance that your data originated in some 16-bit (fixed-width) encoding that was incorrectly converted to UTF8, and there may be better ways of "fixing" the problem than just deleting these odd characters.

    Still, if your experience with the data in question indicates that deletion is the "right thing" to do, it's easy to detect and "fix" the problem:

    # Assuming $string is utf-8 endcoded: if ( my @badch = ( $string =~ /[\x{d800}-\x{dfff}]/g )) { warn sprintf( "%d surrogate code point(s) found\n", scalar @badch +); $string =~ tr/\x{d800}-\x{dfff}//d; }
    (Update - forgot to mention: you can read more about surrogate-pair characters in perlunicode. It may be that your UTF8 data started out as UTF16, but was incorrectly treated as if it were UCS2 when it was converted to UTF8. Converting to UTF8 in the correct way would yield different results.)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-19 05:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found