You are here iC Home > Perl > Web Encoding & Decoding > Generic Web Encoding

Perl

3.1.1 Generic Web Encoding

04.12.2008
3.1 Web Encoding & Decoding [  up  ] - [ a - z ] - [ search PC ] - [ top ] 3.2 Character Encoding

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:



Advanced search tips
3.1 Web Encoding & Decoding [  up  ] - [ top ] 3.2 Character Encoding



[ home ] - [ search ] - [ feedback ]

copyright by reto - created with mytexi