PHP convert_cyr_string() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, convert_cyr_string() converts text between Cyrillic character sets using single-letter charset codes.

String, from, and to are required. Prefer iconv() or mb_convert_encoding() in modern PHP.

Syntax

Call convert_cyr_string with from and to codes. Start with this line.

PHP
$out = convert_cyr_string($text, "w", "k");
CallWhat it does
convert_cyr_string($text, "w", "a")Converts between Cyrillic charsets.
iconv("Windows-1251", "KOI8-R", $text)Modern alternative with iconv().

This function is deprecated in newer PHP versions. Prefer iconv or mbstring.

Library declarations
PHP
convert_cyr_string(string $str, string $from, string $to): string

Parameters

ParameterDescription
strRequired. The Cyrillic text to convert.
fromRequired. Source charset code (one letter).
toRequired. Target charset code (one letter).

Return Value

Return ValueDescription
stringThe converted string.

PHP Examples

Example 1 — KOI8-R to Windows-1251 (Legacy)

Classic API shape. Run only where convert_cyr_string still exists (PHP < 8).

PHP
<?php
// Requires PHP < 8.0
$koi8 = /* bytes already in KOI8-R */;
$win = convert_cyr_string($koi8, "k", "w");

echo bin2hex($win);

How It Works

  1. Start with KOI8-R bytes. The source must already match "k".
  2. Pick destination "w". That means Windows-1251.
  3. Convert. Returns a new byte string in the target encoding.
  4. PHP 8 warning. This call is undefined on current PHP — migrate to Example 3.

Example 2 — Guard Before Calling

Detect removal safely so tutorials and apps fail gracefully.

PHP
<?php
if (!function_exists("convert_cyr_string")) {
    echo "convert_cyr_string() is not available.\n";
    echo "Use iconv() or mb_convert_encoding() instead.\n";
    exit(0);
}

echo "Legacy function is available on this PHP build.\n";

How It Works

  1. Ask PHP. function_exists checks the current runtime.
  2. PHP 8 path. The function is missing → print guidance and exit cleanly.
  3. Older PHP path. You could call the legacy converter after this guard.
  4. Teaching value. Always version-check removed APIs in shared samples.

Example 3 — Modern Replacement with iconv()

Same intent as convert_cyr_string($str, "k", "w"), using full charset names.

PHP
<?php
$koi8 = /* bytes already in KOI8-R */;

$win = iconv("KOI8-R", "Windows-1251//IGNORE", $koi8);
if ($win === false) {
    fwrite(STDERR, "Conversion failed\n");
    exit(1);
}

echo bin2hex($win), PHP_EOL;

// Alternative:
// $win = mb_convert_encoding($koi8, "Windows-1251", "KOI8-R");

How It Works

  1. Use full names. "KOI8-R" and "Windows-1251" replace letters k / w.
  2. Call iconv. Converts from source encoding to destination.
  3. Check failure. false means bad bytes or missing support.
  4. Optional mbstring. mb_convert_encoding is the other common modern API.

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 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 for encodings (for example "k" for KOI8-R and "w" for Windows-1251).
No. It was deprecated in PHP 7.4 and removed in PHP 8.0. Use iconv() or mb_convert_encoding() in modern PHP.
k = KOI8-R, w = Windows-1251, i or d = ISO-8859-5, a = CP866 (IBM866), m = Mac Cyrillic. Each letter selects a legacy Cyrillic encoding.
convert_cyr_string() expects $str to already be encoded in the $from charset. Passing UTF-8 while claiming KOI8-R source misreads the bytes and garbles the result.

Next: convert_uudecode()

Decode uuencoded text back into binary data.

convert_uudecode() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

8 people found this page helpful