Are there any best practices or alternative approaches to achieve transliteration in PHP without encountering memory exhaustion issues?

Transliteration in PHP can sometimes lead to memory exhaustion issues when processing large amounts of text due to the memory required to store the transliterated output. One way to avoid this problem is to process the text in smaller chunks instead of all at once, thus reducing the memory usage during transliteration.

function transliterateText($text) {
    $chunkSize = 1000;
    $textLength = mb_strlen($text);

    $transliteratedText = '';

    for ($i = 0; $i < $textLength; $i += $chunkSize) {
        $chunk = mb_substr($text, $i, $chunkSize);
        $transliteratedChunk = transliterator_transliterate('Latin-ASCII', $chunk);
        $transliteratedText .= $transliteratedChunk;
    }

    return $transliteratedText;
}

// Example usage
$text = "Привет мир!";
$transliteratedText = transliterateText($text);
echo $transliteratedText;