PHP convert_cyr_string() Function

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
Legacy / PHP 7

What You’ll Learn

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().

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.

📝 Syntax

The convert_cyr_string() function accepts three parameters:

PHP
string convert_cyr_string(string $str, string $from, string $to)

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

CodeEncoding
kKOI8-R
wWindows-1251
i or dISO-8859-5
aIBM866 (CP866)
mMac OS Cyrillic

Return Value

Returns the converted string on success. The original string is not modified.

⚡ 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

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.

PHP
<?php
// Cyrillic "ABV" (АБВ) in Windows-1251: bytes C0 C1 C2
$win1251 = "\xC0\xC1\xC2";

$koi8r = convert_cyr_string($win1251, "w", "k");

echo "Win-1251 hex: " . bin2hex($win1251) . "\n";
echo "KOI8-R hex:   " . bin2hex($koi8r) . "\n";
?>

How It Works

  • "w" tells PHP the input is Windows-1251; "k" requests KOI8-R output.
  • Each Cyrillic letter maps to a different byte value in each encoding.
  • Using bin2hex() avoids terminal charset display issues.

Example 2 — Round-Trip Conversion

Convert to another encoding and back—you should recover the original bytes.

PHP
<?php
$original = "\xE1\xE2\xE3";  // KOI8-R bytes for АБВ

$temp = convert_cyr_string($original, "k", "w");
$back = convert_cyr_string($temp, "w", "k");

echo "Original: " . bin2hex($original) . "\n";
echo "Back:     " . bin2hex($back) . "\n";
echo "Match:    " . ($back === $original ? "yes\n" : "no\n");
?>

How It Works

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";
?>

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.

PHP
<?php
$koi8r = "\xE1\xE2\xE3";  // KOI8-R bytes for АБВ

$utf8 = iconv("KOI8-R", "UTF-8//IGNORE", $koi8r);

echo "KOI8-R hex: " . bin2hex($koi8r) . "\n";
echo "UTF-8 text: $utf8\n";
echo "UTF-8 hex:  " . bin2hex($utf8) . "\n";
?>

How It Works

iconv() accepts human-readable charset names and supports UTF-8. Use //IGNORE or //TRANSLIT suffixes to handle unmappable characters.

Example 5 — Modern Replacement with mb_convert_encoding()

The mbstring extension provides another PHP 8-compatible path to UTF-8.

PHP
<?php
$win1251 = "\xC0\xC1\xC2";  // Windows-1251 АБВ

$utf8 = mb_convert_encoding($win1251, "UTF-8", "Windows-1251");

echo "Win-1251 hex: " . bin2hex($win1251) . "\n";
echo "UTF-8 text:   $utf8\n";

// Convert back to Windows-1251
$back = mb_convert_encoding($utf8, "Windows-1251", "UTF-8");
echo "Round-trip:   " . ($back === $win1251 ? "yes\n" : "no\n");
?>

How It Works

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.

🚀 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.

📝 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.

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+.

💡 Best Practices

✅ Do

  • Use iconv() or mb_convert_encoding() 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

Key Takeaways

Knowledge Unlocked

Five things to remember about convert_cyr_string()

Essential points for legacy Cyrillic encoding work in PHP.

5
Core concepts
🔄 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.

Explore More PHP String Functions

Continue with count_chars(), explode(), and the rest of the string function reference.

Next: convert_uudecode() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful