develooper Front page | perl.beginners | Postings from May 2007

RE: Convert german umlaut to ascii

Thread Previous | Thread Next
From:
Thomas Bätzler
Date:
May 21, 2007 01:41
Subject:
RE: Convert german umlaut to ascii
Message ID:
E350C77ABFDBD242BDD51F5DA07905C101887676@SONNE.gw.bringe.net
 
Martin Barth <martin@senfdax.de> suggested:

> On Mon, 21 May 2007 08:41:13 +0200
> Andreas Moroder <andreas.moroder@sb-brixen.it> wrote:
> 
> > Hello,
> > 
> > in our application I have to convert all german
> > Umlaute in a string to 
> > a two char combination ä to ae, Ö to OE and so on.
> > 
> > Can anyone please tell me how to do this ?

> for example:
> 
> % echo "äpfel klöster übelkeit" | perl -ple 's/ä/ae/; 
> s/ü/ue/; s/ö/oe/;'
> aepfel kloester uebelkeit

I would suggest you use a hash to map your conversions:

#!/usr/bin/perl -w

use strict;

my %map = ( 'ä' => 'ae', 'Ä' => 'Ae',
		'ö' => 'oe', 'Ö' => 'Oe',
		'ü' => 'ue', 'Ü' => 'Ue',
		'ß' => 'ss' ); # add needed conversions!

my $test = "Äpfel Klöster Übelkeit";

my $in =  '[' . join( '', keys %map ) . ']';

$test =~ s/($in)/$map{$1}/eg;

print "$test\n";

HTH,
Thomas

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About