in reply to HTML::Strip and UTF8 -- is there some way I can just skip all the "UTF8 only" entities?

HTML::Strip's parse returns a string of (unicode) characters. You can't write a string of characters to a stream of bytes (such as STDOUT). The characters must be encoded first. That's what the warning you're getting means. encode in Encode can be used to perform that task.

So what happens when you try to encode a character that doesn't exist in the target character set? It gets replaced, often with a question mark ("?"). If that ok, then check out the following snippet.

use 5.008000; use strict; use warnings; use Test::More 'no_plan'; use Encode qw( encode ); use HTML::Strip qw( ); use constant ENCODING => 'iso-latin-1'; sub html_to_text { my ($html) = @_; my $stripper = HTML::Strip->new(); return $stripper->parse($html); } sub chars_to_bytes { my ($encoding, $text) = @_; return encode($encoding, $text); } { foreach ( [ 'blah', 'blah' ], [ '&Uuml --', 'Ü --' ], [ 'blah -- ’ -- blah', 'blah -- ? -- blah' ], [ 'Ü -- ’ -- blah', 'Ü -- ? -- blah' ], ) { my $html = $_->[0]; my $expect = $_->[1]; my $text = chars_to_bytes(ENCODING, html_to_text($html)); is($text, $expect, $html); } }

If question marks are not ok, you can specify your own replacement character, including nothing.

use 5.008000; use strict; use warnings; use Test::More qw( no_plan ); use Encode qw( encode ); use HTML::Strip qw( ); use constant ENCODING => 'iso-latin-1'; sub html_to_text { my ($html) = @_; my $stripper = HTML::Strip->new(); return $stripper->parse($html); } sub chars_to_bytes { my ($encoding, $text) = @_; return encode($encoding, $text, sub { '' }); } { for ( [ 'blah', 'blah' ], [ '&Uuml --', 'Ü --' ], [ 'blah -- ’ -- blah', 'blah -- -- blah' ], [ 'Ü -- ’ -- blah', 'Ü -- -- blah' ], ) { my $html = $_->[0]; my $expect = $_->[1]; my $text = chars_to_bytes(ENCODING, html_to_text($html)); is($text, $expect, $html); } }
  • Comment on Re: HTML::Strip and UTF8 -- is there some way I can just skip all the "UTF8 only" entities?
  • Select or Download Code