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

ตอบกระทู้

รูปแสดงอารมณ์
:icon_plusone: :like: :plusone: :gfb: :-D :) :( :-o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: :angry: :baa: :biggrin:
รูปแสดงอารมณ์อื่นๆ

BBCode เปิด
[img] เปิด
[url] เปิด
[Smile icon] เปิด

กระทู้แนะนำ
   

มุมมองที่ขยายได้ กระทู้แนะนำ: รบกวนพี่ๆ ช่วยแปลง code PHP to Javascript ให้หน่อยครับ

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

โดย touleg » 28/10/2012 12:44 am

ทำไม PHP ต้องเขียนเยอะแบบนั้นเลยหรอ
ใช้แค่
string iconv ( string $in_charset , string $out_charset , string $str )

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

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

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

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

โดย tohkai_php » 06/06/2012 10:54 am

รอดูครับ

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

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

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

โดย fa12mkungz » 06/06/2012 9:58 am

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

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;
}

ข้างบน