note 94143 added to ref.iconv

From: Date: Mon, 19 Oct 2009 09:05:52 +0000
Subject: note 94143 added to ref.iconv
Groups: php.notes 
Request: Send a blank email to php-notes+get-161872@lists.php.net to get a copy of this message
This is edited functions utf8_to_cp1251 and cp1251_to_utf8. Changes: Check current string encoding. function cp1251_to_utf8($s) { if ((mb_detect_encoding($s,'UTF-8,CP1251')) == "WINDOWS-1251") { $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; } else { return $s; } } function utf8_to_cp1251($s) { if ((mb_detect_encoding($s,'UTF-8,CP1251')) == "UTF-8") { 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; } else { return $s; } } This info is available in my bulgarian blog: http://mpetrov.net/chesto-sreschani-problemi-s-encoding-pri-php ---- Server IP: 69.147.83.197 Probable Submitter: 82.137.108.2 ---- Manual Page -- http://www.php.net/manual/en/ref.iconv.php Edit -- https://master.php.net/note/edit/94143 Del: integrated -- https://master.php.net/note/delete/94143/integrated Del: useless -- https://master.php.net/note/delete/94143/useless Del: bad code -- https://master.php.net/note/delete/94143/bad+code Del: spam -- https://master.php.net/note/delete/94143/spam Del: non-english -- https://master.php.net/note/delete/94143/non-english Del: in docs -- https://master.php.net/note/delete/94143/in+docs Del: other reasons-- https://master.php.net/note/delete/94143 Reject -- https://master.php.net/note/reject/94143 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#161872) next »