0

I have a textarea. After the click on the "submit" the date from the textarea should be send from row to row to a database. But first i want to replace the german special letters (ä,ö,ü,ß).

My problem: It doesn't work. The output is always "ä, ö or ü". But if I replace the variable with a static "ä" (and don't use the data from the textarea), the script is working. If I use the data from the textarea after the explode, the script does not replace the letters.

<form action="kategorie-add.php" method="POST">
    <textarea name="kategorien"></textarea><br>
    KAT-NR: <input type="text" name="genre"><br>
    <input type="submit" name="submit" value="Senden">

</form>
<?php

if($_POST['submit']){

    $msg = explode( "\r\n", $_POST['kategorien'] );
    foreach( $msg as $zeile ){

        $ers = array(

                'Ä' => 'Ae',
                'Ö' => 'Oe',
                'Ü' => 'Ue',
                'ä' => 'ae',
                'ö' => 'oe',
                'ü' => 'ue',
                'ß' => 'ss'
        );
        $PfadDoc = strtr($zeile,$ers);

        //This is working:
        //$PfadDoc = strtr('ä',$ers);

        echo $PfadDoc
    ?>
Barmar
  • 741,623
  • 53
  • 500
  • 612
DwzE
  • 27
  • 1
  • 8

2 Answers2

1

There is a solution: https://www.liketly.com/forum/thread/32385/multibyte-strtr-mb_strtr/

function my_strtr($inputStr, $from, $to, $encoding = 'UTF-8') {
        $inputStrLength = mb_strlen($inputStr, $encoding);

        $translated = '';

        for($i = 0; $i < $inputStrLength; $i++) {
                $currentChar = mb_substr($inputStr, $i, 1, $encoding);

                $translatedCharPos = mb_strpos($from, $currentChar, 0, $encoding);

                if($translatedCharPos === false) {
                        $translated .= $currentChar;
                }
                else {
                        $translated .= mb_substr($to, $translatedCharPos, 1, $encoding);
                }
        }

        return $translated;
}

Does it work for you?

Kirby
  • 2,847
  • 2
  • 32
  • 42
0

mb_* functions can't prossess replacement. You should use iconv() or mb_convert_encoding() before use strtr().

李德康
  • 15
  • 6