use HTML::Entities; my $input = "abc < ä >
Ö & ü xyz "; # ISO-Latin-1
my $encoded;
$encoded = encode_entities($input, "\xA0-\x{FFFD}");
print "$encoded\n";
# now upgrade $input to character string (utf8)
# (by appending some unicode characters)
$input .= "\x{5555} \x{8888}";
$encoded = encode_entities($input, "\xA0-\x{FFFD}");
print "$encoded\n";
## Ö & ü xyz
abc < ä > Ö & ü xyz 啕 袈
####
abc < ä > ##
s/([\xA0-\x{FFFD}])/$char2entity{$1} || num_entity($1)/ge;
####
$encoded = encode_entities($input, "^\x20-\x7E"); # do not encode printable ASCII chars