_replace_special_chars in Helper ausgelagert.
[kivitendo-erp.git] / SL / DB / Helper / ReplaceSpecialChars.pm
1 package SL::DB::Helper::ReplaceSpecialChars;
2
3 use strict;
4 use utf8;
5
6 use parent qw(Exporter);
7 our @EXPORT = qw(replace_special_chars);
8
9 use Carp;
10 use Text::Unidecode qw(unidecode);
11
12
13
14
15 sub replace_special_chars {
16   my $text = shift;
17
18   return unless $text;
19
20   my %special_chars = (
21     'ä' => 'ae',
22     'ö' => 'oe',
23     'ü' => 'ue',
24     'Ä' => 'Ae',
25     'Ö' => 'Oe',
26     'Ü' => 'Ue',
27     'ß' => 'ss',
28     '&' => '+',
29     '`' => '\'',
30     );
31
32   map { $text =~ s/$_/$special_chars{$_}/g; } keys %special_chars;
33
34   # for all other non ascii chars 'OLÉ S.L.' and 'Årdberg AB'!
35   $text = unidecode($text);
36
37   return $text;
38 }
39
40 1;
41 __END__
42
43 =pod
44
45 =encoding utf8
46
47 =head1 NAME
48
49 SL::DB::Helper::ReplaceSpecialChars - Helper functions for replacing non-ascii characaters
50
51 =head1 SYNOPSIS
52
53   use SL::DB::Helper::ReplaceSpecialChars qw(replace_special_chars);
54   my $ansi_string = replace_special_chars("Überhaupt, with Olé \x{5317}\x{4EB0}"); # hint perldoc may already convert
55   print $ansi_string;
56   # Ueberhaupt, with Ole Bei Jing
57
58 =head1 FUNCTIONS
59
60 =over 4
61
62 =item C<replace_special_chars $text>
63
64 Given a text string this method replaces the most common german umlaute,
65 transforms '&' to '+' and escapes a single quote (').
66 If there are still some non-ascii chars, we use unidecode to guess
67 a sensible ascii presentation, C<perldoc Text::Unidecode>
68
69 =back
70
71 =head1 BUGS
72
73 Nothing here yet.
74
75 =head1 AUTHOR
76
77 M.Bunkus
78 J.Büren (Unidecode added)
79
80 =cut
81
82