четверг, 5 ноября 2009 г.

Processing UTF-8 Files with Perl

use Encode;

open(IN, "<$ARGV[0]") or die "$!"; # Input as default encoding
my $file = do { local $/; }; # Read file contents into scalar
close(IN);
if ($file =~ /]+encoding[\s\x0d\x0a]*=[\s\x0d\x0a]*['"]utf-?8/i ||
$file =~ /]+charset[\s\x0d\x0a]*=[\s\x0d\x0a]*utf-?8/i) {
$file = decode('utf8', $file);
}

$file =~ s/(.)/asciiize($1)/eg; # Process by char

sub asciiize {
return $_[0] if (ord($_[0]) < 128); # ASCII
return sprintf('&#x%04X;', ord($_[0])); # Non-ASCII
}


print $file;


source: http://ripary.com/utf8.html

UTF8 php mail function

If $to, $subject, $message in UTF8.

$to= "=?utf-8?B?".base64_encode($to)."?=";

$subject = "=?utf-8?B?".base64_encode($subject)."?=";

$headers =
“Content-Type: text/plain; ”
. “charset=UTF-8; format=flowed\n”
. “MIME-Version: 1.0\n”
. “Content-Transfer-Encoding: 8bit\n”
. “X-Mailer: PHP\n”;

mail($to, $subject, $message, $headers);

see also: http://geoland.org/2007/12/utf8-ready-php-mail-function/