skip to Main Content

I have a German sentence with "Umlaute" (ä, ö, ü, Ä, Ö, Ü, ß) and want to process it letter by letter. Let’s say I want to write each word backwards.

What I have: "Die Straße enthält viele größere Schlaglöcher"
What I want: "eiD eßartS tlähtne eleiv ereßörg rehcölgalhcS"

I tried to explode the sentence to an array consisting of single words:

$MyText = "Die Straße enthält viele größere Schlaglöcher";
$Words = preg_split(@"/[^wäöüÄÖÜß]/", $MyText);

But as soon as I try to iterate the $Words array I have a problem because it contains letters ("ä", "ö", "ü", …) that are represented by 2 bytes (UTF8) and writing them backwards does not work!

2

Answers


  1. Chosen as BEST ANSWER

    One solution is like the following:

    1. Convert the UTF8-String to a Unicode-String (UTF16) where every letter consists of 2 bytes
    2. Split that string into 2-byte-chunks => $LetterArray
    3. Do something with $LetterArray
    4. Combine the changed array to a new string
    5. Convert the new string back to UTF8

    Here comes a code snippet how this could be done.

    $Word = "enthält";
    $Word_Unicode = mb_convert_encoding($Word, 'UTF-16LE', 'UTF-8');
    $Letters = str_split($Word_Unicode, 2);
    $Letters = array_reverse($Letters);
    $NewWord_Unicode = implode("", $Letters);
    $NewWord = mb_convert_encoding($NewWord_Unicode, 'UTF-8', 'UTF-16LE');
    

    Try it here: https://onlinephp.io/c/69a20


    • You can use the u modifier (ungreedy) in preg_split
    • Then use mb_str_split to slice each word into an array of letters
    • Reverse the sliced word
    • Then join it all together

    $MyText = "Die Straße enthält viele größere Schlaglöcher";
    $Words = preg_split("/[^w]/u", $MyText);
    
    $reversed = [];
    foreach($Words as $word){
        $str_split = mb_str_split($word, 1);
    
        $reversed[] = implode('', array_reverse($str_split) );
    }
    
    $reversed_str = implode(' ', $reversed);
    
    echo $reversed_str;
    

    https://www.php.net/manual/en/reference.pcre.pattern.modifiers.php

    https://www.php.net/manual/en/function.mb-str-split.php

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search