Yamen Nassif
Yamen Nassif

Reputation: 2476

Replacing German chars with Umlaute to simple latin chars php

I am using symfony with simple straight forward code trying to change any special character like ä Ä ö Ö ü Ü to the a A o O u U. I tried everything i could find

normalizer

echo \Normalizer::normalize("ä");

htmlspecialchars

echo htmlspecialchars("ä");

and some others solutioin alike these found online but none of them works for me all will output

ä

and not

a

not only with ä but with all the other chars i mentioned before.

what am I doing wrong ? or is there any other solution to do this.

Upvotes: 5

Views: 6216

Answers (3)

Michael Käfer
Michael Käfer

Reputation: 1786

I guess what you want is to transliterate a string - no matter which special characters it may contain - to a string which only contains characters of the ASCII encoding.

Even if you use PHP's Normalizer, Transliterator and ext-iconv (like the other answers say) this can be complex.

I use symfony/string (first released in 2019) which does all that for you:

use Symfony\Component\String\UnicodeString;

$myString = new UnicodeString('Ä ö Ö ü Ü');

echo $myString->ascii(); // 'A o O u U'

Upvotes: 5

Yamen Nassif
Yamen Nassif

Reputation: 2476

This one worked for me for a further reference

$inputString = "Á,Â,Ã,Ä,Å,Æ,Ç,È,É,Ê,Ë,Ì,Í,Î,Ï,Ð,Ñ,Ò,Ó,Ô,Õ,Ö,×,Ù,Ú,Û,Ü,Ý,Þ,ß,à,á,â,ã,ä,å,æ,ç,è,é,ê,ë,ì,í,î,ï,ð,ñ,ò,ó,ô,õ,ö,ù,ú,û,ü,ý,þ,ÿ";
$extraCharsToRemove = array("\"","'","`","^","~");
echo str_replace($extraCharsToRemove,"",iconv("utf-8","ASCII//TRANSLIT",$inputString));

the out put will be

A,A,A,A,A,AE,C,E,E,E,E,I,I,I,I,D,N,O,O,O,O,O,x,U,U,U,U,Y,Th,ss,a,a,a,a,a,a,ae,c,e,e,e,e,i,i,i,i,d,n,o,o,o,o,o,u,u,u,u,y,th,y

check iconv for a further information

Upvotes: 2

Swift - Friday Pie
Swift - Friday Pie

Reputation: 14589

Such replace is erroneous, because it would produce wrong words (quite a number of German words differ only by presence of umlaut.. you can get even some NSFW results..). There is standard, official rule of replacing diacritics:

  • ä → ae
  • ö → oe
  • ü → ue
  • Ä → Ae
  • Ö → Oe
  • Ü → Ue
  • ß → ss (or SZ for capital)

Normalizer::normalize (normalizer_normalize) from your example is originally from java and in PHP it supported only from certain version. Do you realize that you enter Unicode character into your script code? There is example from manual:

<?php
$char_A_ring = "\xC3\x85"; // 'LATIN CAPITAL LETTER A WITH RING ABOVE' (U+00C5)
$char_combining_ring_above = "\xCC\x8A";  // 'COMBINING RING ABOVE' (U+030A)

$char_1 = Normalizer::normalize( $char_A_ring, Normalizer::FORM_C );
$char_2 = Normalizer::normalize( 'A' . $char_combining_ring_above, Normalizer::FORM_C );

echo urlencode($char_1);
echo ' ';
echo urlencode($char_2);
?> 

(from http://php.net/manual/en/normalizer.normalize.php)

Upvotes: 2

Related Questions