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
One solution is like the following:
Here comes a code snippet how this could be done.
Try it here: https://onlinephp.io/c/69a20
u
modifier (ungreedy) in preg_splitmb_str_split
to slice each word into an array of lettershttps://www.php.net/manual/en/reference.pcre.pattern.modifiers.php
https://www.php.net/manual/en/function.mb-str-split.php