Author Topic: Mysql utf-8, content cp1251 / Проблема с кодировкой мускула  (Read 3050 times)

0 Members and 1 Guest are viewing this topic.

Offline Yan

  • Pre-Newbie
  • Posts: 7
    • View Profile
I have a problem. My Mysql server use default character utf-8, and I use the cp1251 character
How do I prescribe in a script, equating to character cp1251?

This example  for the forum vb
mysql_query ("set character_set_client='cp1251'");
mysql_query ("set character_set_results='cp1251'");
mysql_query ("set collation_connection='cp1251_general_ci'");

P.S sorry for my bad English  :oops:

Offline alekseyn1

  • Full Member
  • ***
  • Posts: 102
    • View Profile
    • My Project
Try a function shown at the end of this post
http://www.4homepages.de/forum/index.php?topic=26465.msg143800#msg143800


Code: [Select]
function utf8_to_cp1251($s)
        {
            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;
}