Pages

2009-11-03

Convert plain text to HTML&XML escaped entities w/ Perl


sub xmlize {
       # unpack byte-by-byte (set to "U0U*" in case of UTF-8)

        my $format="C*"; 
           my @output;
        while($line=shift @_) {
                my @array=map {
                        if($_>127) {
                                sprintf("&#%04ld;",$_);
                        }
                        else {
                                sprintf("%s",chr($_));
                        }
                }unpack($format,$line);
                push(@output,join "", @array);
        }
        return join "",@output;
}






No comments:

Post a Comment