อ่าน text file หลายแฟ้ม ทั้ง utf-8 และ tis-620 ก็ต้องแปลงกัน

check utf-8 and convert
check utf-8 and convert

เดิมอ่านข้อมูล text file แบบ tis-620 แล้ววันหนึ่ง ก็มีแฟ้มข้อมูล (Data) เป็น utf-8 ที่ต้องใช้ script สำหรับอ่าน utf-8 แล้วก็ต้องกำหนด meta content-type ให้ charset เป็น utf-8

แต่หากต้องการอ่านแฟ้ม utf-8 มาแสดงใน tis-620 ก็ต้องมีการ convert ข้อมูลกันหน่อย เดิมทีนั้น จะอ่านข้อมูลจาก text file แบบ tis-620 ลงอาร์เรย์ แล้วใช้งาน ก็ต้องปรับให้รองรับแฟ้มทั้งแบบ tis-620 และ utf-8

code เดิม คือ

<meta http-equiv=”content-type”
content=”text/html;charset=tis-620″ />
<?php
$f=@file(“data.txt”);
echo $f[0];
?>

code ใหม่ คือ

<meta http-equiv=”content-type”
content=”text/html;charset=tis-620″ />
<?php
$f=@file(“data.txt”);
if (mb_check_encoding(file_get_contents(“data.txt”), ‘UTF-8’)) {
$f[0] = iconv(“utf-8″,”tis-620”,$f[0]);
}
echo $f[0];
?>

แฟ้มที่เป็น utf-8 คือ http://www.thaiall.com/checklist.php

Author: บุรินทร์ รุจจนพันธุ์

I am Lecturer, Developer, Researcher, Columnist, Writer, Photographer, and Webmaster - L@mpang man

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.