|
|
 |
convert_cyr_string (PHP 3 >= 3.0.6, PHP 4, PHP 5) convert_cyr_string --
Convert from one Cyrillic character set to another
Descriptionstring convert_cyr_string ( string str, string from, string to )
This function returns the given string converted from one
Cyrillic character set to another. The from
and to arguments are single characters that
represent the source and target Cyrillic character sets. The
supported types are:
k - koi8-r
w - windows-1251
i - iso8859-5
a - x-cp866
d - x-cp866
m - x-mac-cyrillic
Note: This function is
binary-safe.
User Contributed Notes
convert_cyr_string
artyomch at coolfold dot com
26-Apr-2005 03:38
I needed a code for taking UTF8 encoded string from DB and printing it in Win1251 encoded HTML. The problem was that I had to print not just english & cyrillic characters, but all characters stored in UTF encoded string (in my case the DB contained english, russian & hebrew characters).
After reading carefully the UTF8 manual, I've written the following code, that converts all non-win1251 characters into html entities (&#XXXX;).
function utf8_2_win1251 ($str_src)
{
$str_dst = "";
$i = 0;
while ($i<strlen($str_src))
{
$code_dst = 0;
$code_src1 = ord($str_src[$i]);
$i++;
if ($code_src1<=127)
{
$str_dst .= chr($code_src1);
continue;
}
else
if (($code_src1 & 0xE0) == 0xC0)
{
$code_src2 = ord($str_src[$i++]);
if (($code_src2 & 0xC0) != 0x80)
continue;
$code_dst = ( ($code_src1 & 0x1F) << 6) + ($code_src2 & 0x3F);
}
else
if (($code_src1 & 0xF0) == 0xE0)
{
$code_src2 = ord($str_src[$i++]);
if (($code_src2 & 0xC0) != 0x80)
continue;
$code_src3 = ord($str_src[$i++]);
if (($code_src3 & 0xC0) != 0x80)
continue;
$code_dst = ( ($code_src1 & 0x1F) << 12) + ( ($code_src2 & 0x3F) << 6) + ($code_src3 & 0x3F);
}
else
if (($code_src1 & 0xF8) == 0xF0)
{
$code_src2 = ord($str_src[$i++]);
if (($code_src2 & 0xC0) != 0x80)
continue;
$code_src3 = ord($str_src[$i++]);
if (($code_src3 & 0xC0) != 0x80)
continue;
$code_src4 = ord($str_src[$i++]);
if (($code_src4 & 0xC0) != 0x80)
continue;
$code_dst = ( ($code_src1 & 0x1F) << 18) + ( ($code_src2 & 0x3F) << 12) + ( ($code_src3 & 0x3F) << 6) + ($code_src4 & 0x3F);
}
else
{
continue;
}
if ($code_dst)
{
if ($code_dst==0x401)
{
$str_dst .= "¨";
}
else
if ($code_dst==0x451)
{
$str_dst .= "¸";
}
else
if ( ($code_dst>=0x410) && ($code_dst<=0x44F) )
{
$str_dst .= chr ($code_dst-848);
}
else
$str_dst .= "&#{$code_dst};";
}
}
return $str_dst;
}
felix[at]tvpro.net.ru
13-Feb-2005 02:57
Check this code -- exelent to convert win-1251 to UTF-8
just one fix!!!
if ($c==184) { $t.=chr(209).chr(145); continue; };
Anything more it is not necessary.
It is grateful to threed [at] koralsoft.com
28-Jul-2003 03:37
i tried all functions here to convert from cp1251 to unicode, but they don't work. i think that this work :
<?php
function win3utf($s) {
for($i=0, $m=strlen($s); $i<$m; $i++) {
$c=ord($s[$i]);
if ($c<=127) {$t.=chr($c); continue; }
if ($c>=192 && $c<=207) {$t.=chr(208).chr($c-48); continue; }
if ($c>=208 && $c<=239) {$t.=chr(208).chr($c-48); continue; }
if ($c>=240 && $c<=255) {$t.=chr(209).chr($c-112); continue; }
if ($c==184) { $t.=chr(209).chr(209); continue; };
if ($c==168) { $t.=chr(208).chr(129); continue; };
}
return $t;
}
?>
info at newstrack dot ru
02-Feb-2005 06:50
webmaster [ at ] platinumgeneration dot com
11-Jan-2005 08:36
Here's a WORKING function to convert from UTF-8 to Windows-1251, if your hosting provider does not support iconv
<?php
for ($c=0;$c<strlen($s);$c++){
$i=ord($s[$c]);
if ($i<=127) $out.=$s[$c];
if ($byte2){
$new_c2=($c1&3)*64+($i&63);
$new_c1=($c1>>2)&5;
$new_i=$new_c1*256+$new_c2;
if ($new_i==1025){
$out_i=168;
} else {
if ($new_i==1105){
$out_i=184;
} else {
$out_i=$new_i-848;
}
}
$out.=chr($out_i);
$byte2=false;
}
if (($i>>5)==6) {
$c1=$i;
$byte2=true;
}
}
return $out;
}
?>
standov at cgu dot kiev dot ua
08-Dec-2004 03:05
He is improved function to decode win1251->UTF8
<?php
function win2utf($s){
$c209 = chr(209); $c208 = chr(208); $c129 = chr(129);
for($i=0; $i<strlen($s); $i++) {
$c=ord($s[$i]);
if ($c>=192 and $c<=239) $t.=$c208.chr($c-48);
elseif ($c>239) $t.=$c209.chr($c-112);
elseif ($c==184) $t.=$c209.$c209;
elseif ($c==168) $t.=$c208.$c129;
else $t.=$s[$i];
}
return $t;
}
?>
aeon
27-Dec-2003 10:20
threed's function works great, but the replacement for the letter small io (ё) needs to be changed from
<?php
if ($c==184) { $t.=chr(209).chr(209); continue; };
?>
to
<?php
if ($c==184) { $t.=chr(209).chr(145); continue; };
?>
so, the final working result should look like this:
<?php
function win3utf($s) {
for($i=0, $m=strlen($s); $i<$m; $i++) {
$c=ord($s[$i]);
if ($c<=127) {$t.=chr($c); continue; }
if ($c>=192 && $c<=207) {$t.=chr(208).chr($c-48); continue; }
if ($c>=208 && $c<=239) {$t.=chr(208).chr($c-48); continue; }
if ($c>=240 && $c<=255) {$t.=chr(209).chr($c-112); continue; }
if ($c==184) { $t.=chr(209).chr(209); continue; };
if ($c==168) { $t.=chr(208).chr(129); continue; };
}
return $t;
}
?>
almi at univ dot kiev dot ua
06-Dec-2003 05:43
to convert cyrillic string to UTF-8 you can use icovn() function. It does work!
<?php
echo iconv ('CP1251','UTF-8','some cyr string');
?>
But you should copile your php '--with-iconv=[DIR]'
threed [at] koralsoft.com
28-Jul-2003 06:37
i tried all functions here to convert from cp1251 to unicode, but they don't work. i think that this work :
<?php
function win3utf($s) {
for($i=0, $m=strlen($s); $i<$m; $i++) {
$c=ord($s[$i]);
if ($c<=127) {$t.=chr($c); continue; }
if ($c>=192 && $c<=207) {$t.=chr(208).chr($c-48); continue; }
if ($c>=208 && $c<=239) {$t.=chr(208).chr($c-48); continue; }
if ($c>=240 && $c<=255) {$t.=chr(209).chr($c-112); continue; }
if ($c==184) { $t.=chr(209).chr(209); continue; };
if ($c==168) { $t.=chr(208).chr(129); continue; };
}
return $t;
}
?>
checat at chat dot ru
12-May-2003 06:59
german at artexpert dot ee
03-May-2003 07:13
previous bit of code (grmaxim's win_to_utf8 function) didn't work for me, so I wrote my own func to convert from win1251 to utf8:
<?php
function win2utf($s) {
for($i=0,$m=strlen($s);$i<$m;$i++) {
$c=ord($s[$i]);
if ($c>127) if ($c==184) $t.=chr(209).chr(209); elseif ($c==168) $t.=chr(208).chr(129); else $t.=($c>239?chr(209):chr(208)).chr($c-48);
else $t.=$s[$i];
}
return $t;
}
?>
Hope this helps
grmaxim at givc dot ru
17-Apr-2003 10:21
I hope it to you we shall help
<?php
function win_to_utf8($str){
$str = convert_cyr_string($str, 'w','i'); $str = utf8_encode ($str); return $str;
}
function utf8_to_win($str){
$str = utf8_decode ($str); $str = convert_cyr_string($str, 'i','w'); return $str;
}
?>
If there are questions - shall help. Good luck friends!!!
pavel_bashkatov at elkogroup dot com
17-Apr-2003 07:20
To: mihailsbo at lycos dot ru
Transliteration could be done easier:
<?
function transliterate($cyrstr)
{
$ru = array('A', 'a',
'?', '?',
'?', '?',
'?', '?',
'?', '?',
'?', '?',
'?', '?',
'?', '?',
'?', '?',
'?', '?',
'?', '?',
'?', '?',
'?', '?',
'?', '?',
'?', '?',
'?', '?',
'?', '?',
'?', '?',
'?', '?',
'?', '?',
'?', '?',
'?', '?',
'?', '?',
'?', '?',
'?', '?',
'?', '?',
'?', '?',
'?', '?',
'?', '?',
'?', '?',
'?', '?',
'?', '?',
'?', '?');
$en = array('A', 'a',
'B', 'b',
'V', 'v',
'G', 'g',
'D', 'd',
'E', 'e',
'E', 'e',
'Zh', 'zh',
'Z', 'z',
'I', 'i',
'J', 'j',
'K', 'k',
'L', 'l',
'M', 'm',
'N', 'n',
'O', 'o',
'P', 'p',
'R', 'r',
'S', 's',
'T', 't',
'U', 'u',
'F', 'f',
'H', 'h',
'C', 'c',
'Ch', 'ch',
'Sh', 'sh',
'Sch', 'sch',
'\'', '\'',
'Y', 'y',
'\'', '\'',
'E', 'e',
'Ju', 'ju',
'Ja', 'ja');
return str_replace($ru, $en, $cyrstr);
}
?>
mihailsbo at lycos dot ru
03-Mar-2003 05:15
<?php
function transliterate( $text )
{
$cyrlet = 'ÀÁÂÃÄŨÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞß'.
'àáâãä叿çèéêëìíîïðñòóôõö÷øùúûüýþÿ';
$englet = 'ABVGD ZIJKLMNOPRSTUFHC `Y`E '.
'abvgd zijklmnoprstufhc `y`e ';
$result = '';
for ( $i=0; $i<strlen($text); $i++ ) {
$c1 = $text[ $i ];
$p1 = strpos( $cyrlet, $c1 );
if ( $p1 === FALSE ) { $result .= $c1; continue; }
$ct = $englet[ $p1 ];
if ( $ct != ' ' ) { $result .= $ct; continue; }
switch ( $c1 )
{
case 'Å':
$ct = 'Je';
break;
case 'å':
$ct = 'e';
break;
case '¨':
$ct = 'Jo';
break;
case '¸':
$ct = 'jo';
break;
case 'Æ':
$ct = 'Zh';
break;
case 'æ':
$ct = 'zh';
break;
case '×':
$ct = 'Ch';
break;
case '÷':
$ct = 'ch';
break;
case 'Ø':
$ct = 'Sh';
break;
case 'ø':
$ct = 'sh';
break;
case 'Ù':
$ct = 'Sch';
break;
case 'ù':
$ct = 'sch';
break;
case 'Þ':
$ct = 'Ju';
break;
case 'þ':
$ct = 'ju';
break;
case 'ß':
$ct = 'Ja';
break;
case 'ÿ':
$ct = 'ja';
break;
default:
$ct = '?';
}
$result .= $ct;
}
return $result;
}
?>
// P.S. Thanks to PHP developers for rich and convenient
// programming language!
mitya at alesh dot ru
25-Sep-2002 04:21
there is a little script that convert utf to cp1251
<?php
function u8($win,$h,$t) {
global $w8;
$w8[chr($h).chr($t)] = $win;
}
$c1 = chr(208);
$c2 = chr(209);
u8("é",208,185); u8("ö",209,134); u8("ó",209,131);
u8("ê",208,186); u8("å",208,181); u8("í",208,189);
u8("ã",208,179); u8("ø",209,136); u8("ù",209,137);
u8("ç",208,183); u8("õ",209,133); u8("ú",209,138);
u8("ô",209,132); u8("û",209,139); u8("â",208,178);
u8("à",208,176); u8("ï",208,191); u8("ð",209,128);
u8("î",208,190); u8("ë",208,187); u8("ä",208,180);
u8("æ",208,182); u8("ý",209,141); u8("ÿ",209,143);
u8("÷",209,135); u8("ñ",209,129); u8("ì",208,188);
u8("è",208,184); u8("ò",209,130); u8("ü",209,140);
u8("á",208,177); u8("þ",209,142); u8("É",208,153);
u8("Ö",208,166); u8("Ó",208,163); u8("Ê",208,154);
u8("Å",208,149); u8("Í",208,157); u8("Ã",208,147);
u8("Ø",208,168); u8("Ù",208,169); u8("Ç",208,151);
u8("Õ",208,165); u8("Ú",208,170); u8("Ô",208,164);
u8("Û",208,171); u8("Â",208,146); u8("À",208,144);
u8("Ï",208,159); u8("Ð",208,160); u8("Î",208,158);
u8("Ë",208,155); u8("Ä",208,148); u8("Æ",208,150);
u8("Ý",208,173); u8("ß",208,175); u8("×",208,167);
u8("Ñ",208,161); u8("Ì",208,156); u8("È",208,152);
u8("Ò",208,162); u8("Ü",208,172); u8("Á",208,145);
u8("Þ",208,174); u8("¸",209,145); u8("¨",208,129);
function utf2win ($text) {
global $c1,$c2,$w8;
$u = false;
$temp = "";
for($i=0,$len=strlen($text); $i<$len; $i++) {
$c = substr($text,$i,1);
if ($u) {
$c = $w8[$lc.$c];
$temp .= isset($c)?$c:"?";
$u = false;
}
else if ($c==$c1 || $c==$c2) {
$u = true;
$lc = $c;
}
else
$temp .= $c;
}
return $temp;
}
$text = file("utf8.txt");
foreach($text as $line)
echo "<br>".utf2win($line);
?>
how it works you can see on http://www.alesh.ru/lj
But there is another function iconv() (http://php.net/iconv) but not all hosters support iconv
spam at alesh dot ru
12-Jul-2002 10:13
mokvas at freemail dot org dot mk
30-Jul-2001 11:45
Not all of cyrilic characters are supported by this function. Cyrilic chars from Macedonian Alphabet like Sh, kj, dz' ,nj are not supported.
| |