về vấn đề tiếng việt trong PHP

193 views
Skip to first unread message

dthai

unread,
May 27, 2008, 10:39:49 PM5/27/08
to PHPVietnam
Xin chào mọi người,

Cho mình hỏi về 1 cái cơ bản như thế này:

Nếu trong trang HTML ta có dòng code: <meta http-equiv="Content-Type"
content="text/html; charset=UTF-8" />

Thì khi đưa dữ liệu nhập vào PHP, có cần thiết phải sử dụng hàm
utf8_encode để mã hóa dữ liệu sang Unicode không? Hay chỉ cần nhập vào
bình thường là được?

Mình đã thử cách nhập vào bình thường, khi lấy ra thì trình duyệt hiển
thị tốt. Nhưng ko biết làm như vậy có đúng hay không, và lúc đó dữ
liệu nằm trong database có phải là Unicode hay không?

Xin mọi người giúp đỡ,

Thanks nhiều

KENT

unread,
May 27, 2008, 10:52:40 PM5/27/08
to phpvi...@googlegroups.com
:D Nói một cách đơn giản thì nếu trình duyệt của bác hiển thị tốt thì những gì bác đã làm đều đúng cả, bác ko cần phải suy nghĩ nhiều về vấn đề đó.
Việc dữ liệu trong database có phải Unicode hay không thì phải check, nhưng theo em thì nó đang ở dạng Unicode ; )

Btw, Bác cũng có thể lưu dữ liệu dưới dạng NCR Decimal.



2008/5/28 dthai <thaib...@gmail.com>:



--
---
Regards,
Hoang Nguyen

Dinh Ngoc Hung

unread,
May 27, 2008, 10:56:54 PM5/27/08
to phpvi...@googlegroups.com
Tiện cho mình hỏi luôn là khi input vào form là UTF-8 thì làm cách nào để PHP hoặc mySQL tự convert sang NCR Decimal khi lưu không?


Best Regards

Hung, Dinh Ngoc (Mr.)
Project Manager - VHTC Corp.
Addr: No. 3 Doi Cung, Hai Ba Trung Dist., Ha Noi, Viet Nam
            +84 947 85 13 88





Lamx

unread,
May 27, 2008, 11:01:16 PM5/27/08
to phpvi...@googlegroups.com
Bạn thử để Content-Type dạng ISO

2008/5/28 Dinh Ngoc Hung <hung...@gmail.com>:



--
Tks & Rgds
Lam,Nguyen Mai

dungdeveloper

unread,
May 27, 2008, 11:27:57 PM5/27/08
to PHPVietnam
Cái này mình trích từ 1 bài viết về Unicode Support ra , có thể là có
ích cho bạn :

Về phía Mysql thì cần lưu ý :
* What character set and collation is used on database, table and
fields (remember, it can be heterogeneous)
* What character set is used during a client session (and remember it
can be changed using SET NAME)

Về phía PHP :
* PHP doesn't take encoding in account. A string in PHP is and will
always be a sequence of bytes.
* It's easy when you're using strings coming from the database (we
saw how to control the encoding above). But when you type directly
strings in the code, what is the encoding used?
* The answer is : the encoding of the file, and this is controlled by
your favorite code editor.
* Good thing to do now, go to the preferences of your text editor and
be sure that the encoding used is UTF-8.
* You should be able to know at every moment what is the encoding of
the string you're manipulating in PHP.

Về phía Apache và HTML :
When a browser receive some HTML content, how does it know which
encoding is used? There's 3 steps for it :

1. Check the Content-type HTTP header : This header gives the type
of the content, and can also precise which encoding is used
* Apache is in charge of sending the HTTP content-type
header
* Good news, we can manipulate this using header function in
PHP
* Even better, we can set a default value in the php.ini
file for all content sent by it
2. If the header is missing, the browser then check the content-
type meta tag in the HTML document
* We need to be sure that no special character appear before
this tag...
* But we can easily modify this in the templates
3. If it's missing also, the browser will try to auto-detect the
character set used in the page (UTF-8 is easily auto-detected because
of its properties)

* Note that web applications are often serving different type of
content (CSV, PDF, etc...) through Apache
* For these special output, the HTTP header is the only
solution, and should be taken care of manually in the code.

The other part concerning HTML is concerning user input (forms). We
need to be able to control the encoding that will be used for sending
the data of a form back to the server. Here is how the browser is
deciding :

* It looks for an accept-encoding attribute in the form tag, if
it's set, it will use this value as the encoding to send the data
* If it's not set, the browser will use the encoding of the page
to send the data
* Best practice in this case is to always specify the excepted
encoding in accept-encoding attribute of the form, even if it's not
mandatory

Hope it useful ^^

dthai

unread,
May 27, 2008, 11:31:04 PM5/27/08
to PHPVietnam
Mình thấy 2 cái này ko giống nhau:
1. Nếu mình không encode dữ liệu nhập vào, khi display trên trình
duyệt nó hiện ra đúng (vì trình duyệt đang set là UTF-8).
2. Nhưng nếu mình dùng hàm utf8_encode để mã hóa dữ liệu nhập vào -->
lúc này dữ liệu nhập vào là UTF-8, nếu khi xuất ra mình ko dùng hàm
utf8_decode thì trình duyệt không thể hiển thị đúng được. Suy ra dữ
liệu ở trường hợp 1. không phải là UTF-8 mà là 1 dạng mã nào đó !?!

Hix, khó hiểu thiệt, ai có thể nói rõ vụ này or cho mình 1 cái link
tham khảo ngọn nguồn chuyện này dc ko ? T__T ? cảm ơn mấy bạn nhiều
nha

On May 28, 9:52 am, KENT <hoangk...@gmail.com> wrote:
> :D Nói một cách đơn giản thì nếu trình duyệt của bác hiển thị tốt thì những
> gì bác đã làm đều đúng cả, bác ko cần phải suy nghĩ nhiều về vấn đề đó.
> Việc dữ liệu trong database có phải Unicode hay không thì phải check, nhưng
> theo em thì nó đang ở dạng Unicode ; )
>
> Btw, Bác cũng có thể lưu dữ liệu dưới dạng NCR Decimal.
>
> 2008/5/28 dthai <thaibdu...@gmail.com>:
Reply all
Reply to author
Forward
0 new messages