The convert_cyr_string() function converts strings between legacy Cyrillic encodings such as KOI8-R and Windows-1251. It is a historical PHP function—removed in PHP 8—but understanding it helps when reading older code or migrating legacy Russian/Ukrainian text data.
01
Cyrillic Encodings
KOI8-R, Win-1251, etc.
02
Letter Codes
k, w, i, a, m, d.
03
Byte Strings
Input must match $from.
04
PHP 8 Removed
Use iconv() instead.
05
Legacy Data
Old databases & files.
06
Modern Alt
mb_convert_encoding().
Fundamentals
Definition and Usage
In PHP, convert_cyr_string() maps bytes from one Cyrillic character set to another. Unlike UTF-8 conversion, it works with older single-byte encodings common in 1990s–2000s Russian-language software. You pass the raw byte string and two one-letter charset identifiers.
⚠️
Important: Removed in PHP 8.0
This function was deprecated in PHP 7.4 and removed in PHP 8.0. Treat this tutorial as reference for legacy code. In modern PHP, use iconv() or mb_convert_encoding() for all charset work including Cyrillic and UTF-8.
Foundation
📝 Syntax
The convert_cyr_string() function accepts three parameters:
$str — the input string, already encoded in the $from charset (raw bytes, not UTF-8 unless converting from a UTF-8-compatible path via other tools).
$from — single-letter code for the source Cyrillic encoding.
$to — single-letter code for the target Cyrillic encoding.
Charset Letter Codes
Code
Encoding
k
KOI8-R
w
Windows-1251
i or d
ISO-8859-5
a
IBM866 (CP866)
m
Mac OS Cyrillic
Return Value
Returns the converted string on success. The original string is not modified.
Cheat Sheet
⚡ Quick Reference
KOI8-R → Win-1251
convert_cyr_string($s, "k", "w")
Common legacy migration
Win-1251 → KOI8-R
convert_cyr_string($s, "w", "k")
Reverse direction
Modern (iconv)
iconv("KOI8-R", "UTF-8", $s)
PHP 8+ replacement
Modern (mb)
mb_convert_encoding($s, "UTF-8", "KOI8-R")
With mbstring ext
Hands-On
Examples Gallery
These examples teach how convert_cyr_string() worked in PHP 7 and below. On PHP 8+, use the iconv() / mb_convert_encoding() alternatives shown in Examples 4 and 5.
📚 Getting Started
Convert raw bytes between Windows-1251 and KOI8-R (PHP 7 and earlier).
Example 1 — Convert Windows-1251 Bytes to KOI8-R
A correct usage pattern: the input string contains actual Windows-1251 bytes, not UTF-8 text.
Converting KOI8-R → Windows-1251 → KOI8-R restores the original byte sequence when the charsets are true inverses for those characters.
📈 Practical Patterns
Common mistakes and modern replacements every developer should know.
Example 3 — Common Mistake: UTF-8 Text with Wrong $from
The old reference passed UTF-8 Cyrillic to convert_cyr_string() while claiming the source was KOI8-R—that produces garbled output.
PHP
<?php
// This string is UTF-8, NOT KOI8-R — a frequent beginner error
$utf8Text = "Привет, мир!";
$wrong = convert_cyr_string($utf8Text, "k", "i");
echo "UTF-8 bytes: " . bin2hex($utf8Text) . "\n";
echo "Wrong output: " . bin2hex($wrong) . "\n";
echo "Lesson: \$from must match the actual byte encoding of \$str\n";
?>
📤 Output (PHP 7):
UTF-8 bytes: d09fd180d0b8d0b2d0b5d1822c20d0bcd0b8d18021
Wrong output: <garbled bytes — not valid ISO-8859-5 text>
Lesson: $from must match the actual byte encoding of $str
How It Works
UTF-8 uses multiple bytes per Cyrillic letter. KOI8-R uses one byte per letter with completely different values. Always know your source encoding before converting.
Example 4 — Modern Replacement with iconv() (PHP 8+)
Convert legacy KOI8-R bytes to UTF-8 using iconv(), which works in current PHP.
mb_convert_encoding() is ideal when the mbstring extension is enabled. It handles UTF-8 cleanly and is the recommended approach for multilingual web applications today.
Applications
🚀 Common Use Cases
Reading legacy databases — data stored in KOI8-R or Windows-1251 before UTF-8 adoption.
Migrating old PHP code — understand what convert_cyr_string() did before replacing it with iconv().
Flat-file imports — converting Cyrillic text files from Soviet-era encodings.
Email and news (FidoNet) — historical systems that standardized on KOI8-R.
Learning encoding concepts — see why UTF-8 replaced dozens of regional charsets.
🧠 How convert_cyr_string() Works
1
PHP reads the byte string
You pass raw bytes and two one-letter charset codes ($from, $to).
Input
2
Each byte is remapped
PHP looks up each byte in a translation table from the source Cyrillic charset to the target charset.
Transform
3
Converted string is returned
The output has the same number of characters (bytes) as the input for these single-byte encodings.
Output
=
📝
Legacy-compatible text
Bytes ready for an old system expecting the target Cyrillic encoding.
Important
📝 Notes
Removed in PHP 8.0 — do not use in new projects; use iconv() or mb_convert_encoding().
Only converts between the listed legacy Cyrillic encodings—not UTF-8, Latin-1, or general Unicode.
The input string must already be in the $from encoding.
Charset parameters are single characters ("k", not "KOI8-R").
Modern web apps should store and transmit text as UTF-8 end to end.
Wrap Up
Conclusion
The convert_cyr_string() function was PHP’s specialized tool for swapping between legacy Cyrillic byte encodings. It served a real need in older Russian-language software, but UTF-8 and modern conversion functions have made it obsolete.
Learn it to read and migrate legacy code, then use iconv() or mb_convert_encoding() for all new Cyrillic and multilingual work in PHP 8+.
Store all new text as UTF-8 in databases and files
Verify the actual encoding of legacy data before converting
Use bin2hex() to debug byte-level conversions
Replace convert_cyr_string() when upgrading old codebases
❌ Don’t
Use convert_cyr_string() in new PHP 8 projects
Pass UTF-8 strings while specifying KOI8-R as $from
Assume garbled output means the function is “broken”
Mix charset letter codes with full names like "KOI8-R"
Skip encoding headers when outputting converted HTML
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about convert_cyr_string()
Essential points for legacy Cyrillic encoding work in PHP.
5
Core concepts
📝01
Legacy Cyrillic
KOI8-R, Win-1251, etc.
Purpose
🔄02
Letter Codes
k, w, i, a, m.
Syntax
⚠03
PHP 8 Removed
Historical only.
Status
🔒04
Match Encoding
$from = actual bytes.
Critical
⚙05
Use iconv()
Modern replacement.
Today
❓ Frequently Asked Questions
convert_cyr_string() converts a string between legacy Cyrillic single-byte encodings such as KOI8-R, Windows-1251, and ISO-8859-5. You pass the raw byte string plus one-character source and destination charset codes.
string convert_cyr_string(string $str, string $from, string $to). The $from and $to parameters are single letters representing encodings (e.g. "k" for KOI8-R, "w" for Windows-1251).
No. The function was deprecated in PHP 7.4 and removed in PHP 8.0. For modern PHP, use iconv() or mb_convert_encoding() instead.
k = KOI8-R, w = Windows-1251, i or d = ISO-8859-5, a = IBM866 (CP866), m = Mac OS Cyrillic. Each letter selects a legacy Cyrillic encoding.
convert_cyr_string() expects $str to already be encoded in the $from charset. Passing UTF-8 text while specifying "k" (KOI8-R) as the source produces incorrect results because the bytes do not match the declared encoding.
Use iconv($from, $to, $str) or mb_convert_encoding($str, $to, $from) for Cyrillic and general charset conversion. Both work in current PHP versions and support UTF-8.