W3docs

convert_cyr_string()

The convert_cyr_string() function is used to convert a string from one Cyrillic character set to another. The syntax of the convert_cyr_string() function is as

Warning

convert_cyr_string() was deprecated in PHP 7.4 and removed in PHP 8.0. It is a legacy function that should not be used in modern code. For converting between character encodings, use mb_convert_encoding() or iconv() instead — see the migration section below.

This chapter explains what convert_cyr_string() did, why it existed, why it was removed, and what to use in its place. It is reference material for reading or maintaining old PHP code — not a function you should call in anything new.

What convert_cyr_string() did

The function converted a string from one single-byte Cyrillic character set to another. Before UTF-8 became the default, Cyrillic text was stored in several incompatible 8-bit encodings (Windows-1251, KOI8-R, ISO 8859-5, DOS code page 866, and a couple of Mac/main-frame sets). convert_cyr_string() remapped the bytes from one of those legacy sets to another.

Syntax

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

It took three parameters and returned the converted string:

ParameterDescription
$strThe string to convert.
$fromA single character naming the source character set.
$toA single character naming the target character set.

The $from and $to arguments were single-letter codes, not full encoding names:

CodeCharacter set
kKOI8-R
wWindows-1251
iISO 8859-5
a / dx-cp866 (DOS)
mx-mac-cyrillic

Example

<?php
// Legacy code only — this throws a fatal error on PHP 8+.
$converted = convert_cyr_string($str, "w", "k"); // Windows-1251 -> KOI8-R
echo $converted;
?>

Here $str is assumed to contain raw Windows-1251 bytes ("w"), and the function rewrites them as KOI8-R bytes ("k"). The key thing to understand is that convert_cyr_string() operated on raw bytes and assumed the source string was already in the $from encoding. If you fed it a UTF-8 string (the modern default), the output was meaningless garbage (mojibake), because UTF-8 bytes do not correspond to any single legacy Cyrillic set.

Why it was removed

  • It only knew a fixed list of legacy 8-bit Cyrillic encodings — UTF-8 was never supported, so it could not participate in modern, Unicode-based text handling.
  • It silently mangled any input that was not actually in the declared $from encoding.
  • Its job is fully covered by the general-purpose iconv() and mb_convert_encoding() functions, which support hundreds of encodings including UTF-8.

For these reasons it was deprecated in PHP 7.4 and deleted in PHP 8.0. Calling it today raises a fatal Error.

What to use instead

Use mb_convert_encoding() (from the mbstring extension) or iconv(). Both take real encoding names and both support UTF-8:

<?php
// Convert Windows-1251 bytes to UTF-8.
$utf8 = mb_convert_encoding($legacy, "UTF-8", "Windows-1251");

// The same thing with iconv().
$utf8 = iconv("Windows-1251", "UTF-8", $legacy);

// Convert KOI8-R to UTF-8.
$utf8 = mb_convert_encoding($legacy, "UTF-8", "KOI8-R");
?>

Converting to UTF-8 (rather than between two legacy sets) is almost always what you want: once text is UTF-8 it works everywhere in modern PHP, databases, and browsers.

Summary

convert_cyr_string() was a pre-Unicode helper for swapping bytes between legacy Cyrillic encodings. It is gone from PHP 8.0 onward. If you encounter it in old code, replace each call with iconv() or mb_convert_encoding() and migrate your data to UTF-8.

For more on working with text in PHP, see PHP Strings and htmlentities().

Practice

Practice
What does the PHP 'iconv' function do as described in the article?
What does the PHP 'iconv' function do as described in the article?
Was this page helpful?