X
X
X
X
X
Independent of the character translation setting of the host (ISO or UTF).
→ Download/wget files/webenc generic inline pl.txt
#!/usr/bin/perl -w
use strict;
use Unicode::String qw(latin1 utf8);
print &web_enc($ARGV[0]), "\n";
sub web_enc ($) {
return '' unless $_[0];
local $SIG{'__WARN__'} = \&alarm_handler; # install signal handler
my $in = $_[0];
my $text_iso = (utf8($in))->latin1;
my $text_utf8 = (latin1($text_iso))->utf8; # reverse check
my $input = $text_iso;
$input = $in if $in ne $text_utf8; # Is ISO already!
my $enc = '';
for (my $i = 0; $i < length($input); $i++) {
my $ordno = ord substr($input, $i, 1);
$enc .= $ordno > 127 ? sprintf("&#%d;", $ordno) : substr($input, $i, 1);
}
$enc =~ s/ $//;;
$enc;
}
sub alarm_handler () {
#print STDERR "alarm catched!\n";
return;
}
See also: