Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: NCR & CER to UTF-8

by theorbtwo (Prior)
on Nov 01, 2005 at 14:44 UTC ( [id://504593]=note: print w/replies, xml ) Need Help??


in reply to NCR & CER to UTF-8

Firstly, the other entry you got, pointing you at HTML::Entities, is without a doubt the right way, if you are looking to solve the problem. On the other hand, you have looked at it, and rejected it for good reason (which here includes "because I already did it that way and want to try doing it other ways as well"), some advice on how to do it more or less the same way, but better...

s(&#(.*?);){ code goes here }eg;
is how I'd write that. The change to (.*?) instead of ([^;]+) is a matter of asthetics, and a somewhat contriversial one. It makes the regex somewhat slower, possibly, but it avoids repeating the exit condition. Putting on the /g is a matter of correctness -- consider <foo>. Note that you don't want to apply the regex to the same data in a loop -- consider &. The /e is to let the replacement thingy be code... what code I'm going to leave as an exercise to the reader.


Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

Replies are listed 'Best First'.
Re^2: NCR & CER to UTF-8
by vnpenguin (Beadle) on Nov 01, 2005 at 15:44 UTC
    Just tried HTML::Entities:
    #!/usr/bin/perl use HTML::Entities; while(<>){ print decode_entities($_); } exit;
    The input file is here. The output UTF-8 is not readable (see the shot). I missed some thing ? (Perl 5.8.6, Fedora Linux 4, LOCALE UTF-8) Thank you,

      1) When you use perl with data that isn't ASCII, it's generally a good idea to tell perl what encoding you expect the filehandles to use, by using binmode: binmode(\*STDOUT, ':utf8');. By default, even in utf8 locales, STDIN, STDOUT, and STDERR are assumed to be latin-1. If you had a use warnings, you would have gotten a lot of warnings about a wide character in print -- one for every character that wasn't in latin-1.

      2) From that screenshot, it appears that what you have is mostly valid utf8, but the thing you are using to view it expects it to be latin-1, not utf8.

      3) It's dangerous to HTML-unescape text containing unescaped HTML or XML tags; after doing so it is impossible to tell the difference between what was <foo> and &lt;foo&gt;, making tags out of things that were not tags before.

      4) Why aren't you using a HTML parser, such as HTML::TreeBuilder?


      Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

        Hi theorbtwo,

        Thank you so much for your quick reply.

        1) By using binmode, I got correct UTF-8 output now.

        2),3) & 4) You are absolutely right. I need learn more and more about Perl :)

        Thank you for all help.

Log In?
Username:
Password:

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

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

    No recent polls found