I offer some straight-forward code. Nothing really fancy and no Perl modules needed. This is a translation problem which screams hash table and look-up.
#!/usr/bin/perl -w use strict; #this is just a tricky way to make a hash table # #translate something to something screams hash #table - we can get this from a file or wherever, #or just make a %TranslateTable declaration statement.. # #I didn't think that was the point here... my %TranslateTable = map { my ($char, $hmtl) = split} split /\n/, <<'END'; Á &Aacute á &aacute É &Eacute é &eacute Í &Iacute í &iacute Ñ &Ntilde ñ &ntilde Ó &Oacute ó &oacute Ú &Uacute ú &uacute Ü &Uuml ü &uuml ¿ &iquest ¡ &iexcl END my $s = "El supersÁnico de los Indi "; my $xlated = fix_utf8($s); print "$s\n"; print "****translated = $xlated\n"; ## this can be done in fewer lines, and could be faster, ## but I didn't think that was the point either.... ## ## this is simple and straightforward. ## maybe I misunderstood the problem, but even so ## there maybe something that you can use ## sorry, that I don't speak this language, perhaps ## there is a funny goof, if so laugh! OK! sub fix_utf8 { my $in_string = shift; my $newstring =""; foreach my $char (split (//,$in_string) ) { $char = $TranslateTable{$char} if $TranslateTable{$char}; $newstring .= $char; } return $newstring; } __END__ ######### prints El supersÁnico de los Indi ****translated = El supers&Aacutenico de los Indi
Update: hash table gen is easier than first posted.
#!/usr/bin/perl -w use strict; #in this case hash table is easy #to generate...easier than first code.. my %TranslateTable = qw ( Á &Aacute á &aacute É &Eacute é &eacute Í &Iacute í &iacute Ñ &Ntilde ñ &ntilde Ó &Oacute ó &oacute Ú &Uacute ú &uacute Ü &Uuml ü &uuml ¿ &iquest ¡ &iexcl ); my $s = "El supersÁnico de los Indi "; my $xlated = fix_utf8($s); print "$s\n"; print "****translated = $xlated\n"; sub fix_utf8 { my $in_string = shift; my $newstring =""; foreach my $char (split (//,$in_string) ) { $char = $TranslateTable{$char} if $TranslateTable{$char}; $newstring .= $char; } return $newstring; } __END__ ######### prints El supersÁnico de los Indi ****translated = El supers&Aacutenico de los Indi

In reply to Re: utf-8 problem by Marshall
in thread utf-8 problem by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.