Convert ASCII to Unicode

แปลง ASCII ให้เป็น Unicode

พอดีได้รู้หลักการทำ SEO (Search engine optimization) จากกูรูหลายสาขาว่า ให้ทำ Web Directory ก็เลยลองทำดูที่ http://directory.komkid.net

หลังจากปรับแต่งแล้วก็สร้าง Category ไปได้ซัก 20 กว่าตัว ก็รู้สึกว่า มันไม่ใช่ นี่มันไม่ใช่ผม… ต้องมานั่งทำทีละอันอย่างนี้ ไม่ใช่แล้ว ยัดใส่ database ด้วย SQL เลยดีกว่า

เริ่มด้วย Excel คือ copy Category ชาวบ้านเค้ามา บวกกับดู table category ของเราแล้วใส่สูตรเพื่อสร้างคำสั่ง SQL ดังรูป

excel_for_sql

พอลอง run ดูปรากฏว่าไม่สำเร็จ เพราะ database เราเก็บเป็นแบบ Unicode นี่เอง

phpmyadmin_sql

ถ้าอย่างงั้นก็ต้องเปลี่ยน ASCII ของเราให้เป็น Unicode ก่อนยัดลง database ซึ่งไหน ๆ ก็ ไหน ๆ แล้ว สร้าง tools ไว้ใช้ถาวรซะเลย โดยสร้าง html ขึ้นมาไฟล์นึง โดยสร้าง form ไว้ ตาม code

1
2
3
4
5
6
7
8
9
10
11
12
13
<form name=”form1″ action=">
<br />
<b>Input Text:</b><br />
<BR><textarea name=”inputtext” rows=10cols=50″></textarea>
<BR><input type=”button” value=”Convert to Unicode” onclick=”Ascii2Unicode()/>
<input type=”button” value=”Convert to ASCII” onclick=”Unicode2Ascii()/>
<br />
Output:<BR>
<span id=”output”></span>
<BR><textarea name=”outputtext” rows=10cols=50″></textarea>
<br /><br /><br />
<input type=”reset” value=Clearonclick=”document.getElementById(’output’).innerHTML = ”” />
</form>

ได้หน้าตาตามรูป
ascii_to_unicode_form
ส่วน Java Script สำหรับทำงานก็ตามนี้

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
function Ascii2Unicode(){
  if(document.forms[0].inputtext.value != ''){
     var strInput = document.forms[0].inputtext.value;
     var strOutput = toUnicode(strInput);
     document.forms[0].outputtext.value = strOutput;
  }
}
function Unicode2Ascii(){
  if(document.forms[0].inputtext.value != ''){
     var strInput = document.forms[0].inputtext.value;
     var strOutput = toAscii(strInput);
     document.getElementById("output").innerHTML =strOutput;
  }
}
function toUnicode(strInput) {
  result = '';
  for (i=0; i<strInput.length; i++) {
    if(strInput.charCodeAt(i) == 10)
        result += '\n';
    else
        result += '&#' + strInput.charCodeAt(i) + ';';
  }
  return result;
}
function toAscii(strInput) {
     replace = '';
      var text = escape(strInput);
      if(text.indexOf('%0D%0A') > -1){
            replace = /%0D%0A/g ;
      }else if(text.indexOf('%0A') > -1){
            replace = /%0A/g ;
      }else if(text.indexOf('%0D') > -1){
            replace = /%0D/g ;
      }
      return unescape( text.replace(replace,'<br />') );
}

ผลที่ได้ก็หน้าตาแบบนี้
ascii_to_unicode_result
โดยลองได้ ที่นี่
พอได้ก็เอาไปใส่ใน Excel ได้ SQL มาก็เอาไป run ใน Database โลด (ประหยัดเวลาได้มิใช่น้อย)

Written by admin on December 3rd, 2008 with 2 comments.
Read more articles on Database and Internet and Programming.

Related articles

2 comments

Read the comments left by other users below, or:

Get your own gravatar by visiting gravatar.com Kelly Brown
#1. June 13th, 2009, at 8:39 AM.

Great post! I’ll subscribe right now wth my feedreader software!

Get your own gravatar by visiting gravatar.com JaneRadriges
#2. June 14th, 2009, at 4:32 AM.

The article is usefull for me. I’ll be coming back to your blog.

Leave your comment...

If you want to leave your comment on this article, simply fill out the next form:

You have to be identified to write a comment.