Thread HTML::Entities - decode_entities() erzeugt kein Unicode sondern Latin1 (40 answers)
Opened by GwenDragon at 2024-03-19 12:49

haj
 2024-03-22 00:09
#195966 #195966
User since
2015-01-07
531 Artikel
BenutzerIn

user image
2024-03-21T18:14:18 GwenDragon
Ich benutze eigentlich immer Encode::decode('UTF-8', ...) wenn die Datei UTF-8 ist.
Da ist doch dann wohl das Pragma CGI qw/-utf8/ obsolet, oder.

Es geht nicht um die Datei, sondern um den $cgi->param. Entweder musst Du den decodieren oder use CGI qw/-utf8/; verwenden.

Ich habe mal ein vollständiges Programm um Deine Datei gestrickt.
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use 5.024;
use autodie;
use Encode qw( decode encode );
use HTML::Entities;
use URI::Escape;

open (my $fh,'<','t.txt');
my $content;
{
    local $/ = undef;
    $content = <$fh>;
}

$content = decode('UTF-8',$content,Encode::FB_CROAK);
$content = decode_entities($content);

my @terms = (
    "\N{LATIN SMALL LETTER A WITH DIAERESIS}",
    "\x{e4}",
    chr 228,
    decode('UTF-8',"\xc3\xa4"),
    decode('UTF-8',uri_unescape("%c3%a4")),
);

for my $term (@terms) {
    my $for_terminal = encode('UTF-8',$term);
    while ($content =~ /$term/g) {
        say "Treffer: '$for_terminal' an Position " . pos $content;
    }
}

Die Ausgabe:
Code: (dl )
1
2
3
4
5
6
7
8
9
10
Treffer: 'ä' an Position 81
Treffer: 'ä' an Position 108
Treffer: 'ä' an Position 81
Treffer: 'ä' an Position 108
Treffer: 'ä' an Position 81
Treffer: 'ä' an Position 108
Treffer: 'ä' an Position 81
Treffer: 'ä' an Position 108
Treffer: 'ä' an Position 81
Treffer: 'ä' an Position 108


In allen fünf Varianten, den $term hinzuschreiben, liefert sowohl die umgewandelte Entity wie auch das explizite ä in "Entität" einen Treffer.

Wie mein Mathelehrer in solchen Fällen zu sagen pflegte: Man kann sich des richtigen Ergebnisses kaum erwehren!

View full thread HTML::Entities - decode_entities() erzeugt kein Unicode sondern Latin1