หน้า 1 จากทั้งหมด 1

รบกวนพี่ๆ ช่วยแปลง code PHP to Javascript ให้หน่อยครับ

โพสต์แล้ว: 06/06/2012 9:58 am
โดย fa12mkungz

โค้ด: เลือกทั้งหมด

function tis620_to_utf8($tis) {
  for( $i=0 ; $i< strlen($tis) ; $i++ ){
    $s = substr($tis, $i, 1);
    $val = ord($s);
    if( $val < 0x80 ){
	 $utf8 .= $s;
    } elseif ((0xA1 <= $val and $val <= 0xDA) 
              or (0xDF <= $val and $val <= 0xFB))  {
	 $unicode = 0x0E00 + $val - 0xA0;
	 $utf8 .= chr( 0xE0 | ($unicode >> 12) );
	 $utf8 .= chr( 0x80 | (($unicode >> 6) & 0x3F) );
	 $utf8 .= chr( 0x80 | ($unicode & 0x3F) );
    }
  }
return $utf8;
}

โค้ด: เลือกทั้งหมด

function utf8_to_tis620($string) {
  $str = $string;
  $res = "";
  for ($i = 0; $i < strlen($str); $i++) {
	if (ord($str[$i]) == 224) {
	  $unicode = ord($str[$i+2]) & 0x3F;
	  $unicode |= (ord($str[$i+1]) & 0x3F) << 6;
	  $unicode |= (ord($str[$i]) & 0x0F) << 12;
	  $res .= chr($unicode-0x0E00+0xA0);
	  $i += 2;
	} else {
	  $res .= $str[$i];
	}
  }
  return $res;
}

Re: รบกวนพี่ๆ ช่วยแปลง code PHP to Javascript ให้หน่อยครับ

โพสต์แล้ว: 06/06/2012 10:54 am
โดย tohkai_php
รอดูครับ

น่าสนใจ ด้วยหลักการแปลง tis-620 เป็น ASCII ก่อน แล้วอ่านออกเป็น utf-8

เห็นมีฟังชั่น ascii to utf-8 พอได้ แต่แปลง tis-620 เป็น ascii นี่สิ ทำไงหว่า เหอๆ

Re: รบกวนพี่ๆ ช่วยแปลง code PHP to Javascript ให้หน่อยครับ

โพสต์แล้ว: 28/10/2012 12:44 am
โดย touleg
ทำไม PHP ต้องเขียนเยอะแบบนั้นเลยหรอ
ใช้แค่
string iconv ( string $in_charset , string $out_charset , string $str )

iconv("UTF-8", "tis-620", $text)

iconv("tis-620","UTF-8", $text)

แค่นี้ก้ได้แระสำหรับ PHP