Release 1.6.1, merged in 931 to HEAD.
[htmlpurifier.git] / docs / enduser-utf8.html
blob6d03ad91c70ccfc7c5d4753264ff8f3cff165346
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
4 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head>
5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6 <meta name="description" content="Describes the rationale for using UTF-8, the ramifications otherwise, and how to make the switch." />
7 <link rel="stylesheet" type="text/css" href="./style.css" />
8 <script defer="defer" type="text/javascript" src="./toc-gen.js"></script>
9 <style type="text/css">
10 .minor td {font-style:italic;}
11 </style>
13 <title>UTF-8: The Secret of Character Encoding - HTML Purifier</title>
15 <!-- Note to users: this document, though professing to be UTF-8, attempts
16 to use only ASCII characters, because most webservers are configured
17 to send HTML as ISO-8859-1. So I will, many times, go against my
18 own advice for sake of portability. -->
20 </head><body>
22 <h1>UTF-8: The Secret of Character Encoding</h1>
24 <div id="filing">Filed under End-User</div>
25 <div id="index">Return to the <a href="index.html">index</a>.</div>
26 <div id="home"><a href="http://htmlpurifier.org/">HTML Purifier</a> End-User Documentation</div>
28 <p>Character encoding and character sets are not that
29 difficult to understand, but so many people blithely stumble
30 through the worlds of programming without knowing what to actually
31 do about it, or say &quot;Ah, it's a job for those <em>internationalization</em>
32 experts.&quot; No, it is not! This document will walk you through
33 determining the encoding of your system and how you should handle
34 this information. It will stay away from excessive discussion on
35 the internals of character encoding.</p>
37 <p>This document is not designed to be read in its entirety: it will
38 slowly introduce concepts that build on each other: you need not get to
39 the bottom to have learned something new. However, I strongly
40 recommend you read all the way to <strong>Why UTF-8?</strong>, because at least
41 at that point you'd have made a conscious decision not to migrate,
42 which can be a rewarding (but difficult) task.</p>
44 <blockquote class="aside">
45 <div class="label">Asides</div>
46 <p>Text in this formatting is an <strong>aside</strong>,
47 interesting tidbits for the curious but not strictly necessary material to
48 do the tutorial. If you read this text, you'll come out
49 with a greater understanding of the underlying issues.</p>
50 </blockquote>
52 <h2>Table of Contents</h2>
54 <ol id="toc">
55 <li><a href="#findcharset">Finding the real encoding</a></li>
56 <li><a href="#findmetacharset">Finding the embedded encoding</a></li>
57 <li><a href="#fixcharset">Fixing the encoding</a><ol>
58 <li><a href="#fixcharset-none">No embedded encoding</a></li>
59 <li><a href="#fixcharset-diff">Embedded encoding disagrees</a></li>
60 <li><a href="#fixcharset-server">Changing the server encoding</a><ol>
61 <li><a href="#fixcharset-server-php">PHP header() function</a></li>
62 <li><a href="#fixcharset-server-phpini">PHP ini directive</a></li>
63 <li><a href="#fixcharset-server-nophp">Non-PHP</a></li>
64 <li><a href="#fixcharset-server-htaccess">.htaccess</a></li>
65 <li><a href="#fixcharset-server-ext">File extensions</a></li>
66 </ol></li>
67 <li><a href="#fixcharset-xml">XML</a></li>
68 <li><a href="#fixcharset-internals">Inside the process</a></li>
69 </ol></li>
70 <li><a href="#whyutf8">Why UTF-8?</a><ol>
71 <li><a href="#whyutf8-i18n">Internationalization</a></li>
72 <li><a href="#whyutf8-user">User-friendly</a></li>
73 <li><a href="#whyutf8-forms">Forms</a><ol>
74 <li><a href="#whyutf8-forms-urlencoded">application/x-www-form-urlencoded</a></li>
75 <li><a href="#whyutf8-forms-multipart">multipart/form-data</a></li>
76 </ol></li>
77 <li><a href="#whyutf8-support">Well supported</a></li>
78 <li><a href="#whyutf8-htmlpurifier">HTML Purifiers</a></li>
79 </ol></li>
80 <li><a href="#migrate">Migrate to UTF-8</a><ol>
81 <li><a href="#migrate-db">Configuring your database</a><ol>
82 <li><a href="#migrate-db-legit">Legit method</a></li>
83 <li><a href="#migrate-db-binary">Binary</a></li>
84 </ol></li>
85 <li><a href="#migrate-editor">Text editor</a></li>
86 <li><a href="#migrate-bom">Byte Order Mark (headers already sent!)</a></li>
87 <li><a href="#migrate-fonts">Fonts</a><ol>
88 <li><a href="#migrate-fonts-obscure">Obscure scripts</a></li>
89 <li><a href="#migrate-fonts-occasional">Occasional use</a></li>
90 </ol></li>
91 <li><a href="#migrate-variablewidth">Dealing with variable width in functions</a></li>
92 </ol></li>
93 <li><a href="#externallinks">Further Reading</a></li>
94 </ol>
96 <h2 id="findcharset">Finding the real encoding</h2>
98 <p>In the beginning, there was ASCII, and things were simple. But they
99 weren't good, for no one could write in Cryllic or Thai. So there
100 exploded a proliferation of character encodings to remedy the problem
101 by extending the characters ASCII could express. This ridiculously
102 simplified version of the history of character encodings shows us that
103 there are now many character encodings floating around.</p>
105 <blockquote class="aside">
106 <p>A <strong>character encoding</strong> tells the computer how to
107 interpret raw zeroes and ones into real characters. It
108 usually does this by pairing numbers with characters.</p>
109 <p>There are many different types of character encodings floating
110 around, but the ones we deal most frequently with are ASCII,
111 8-bit encodings, and Unicode-based encodings.</p>
112 <ul>
113 <li><strong>ASCII</strong> is a 7-bit encoding based on the
114 English alphabet.</li>
115 <li><strong>8-bit encodings</strong> are extensions to ASCII
116 that add a potpourri of useful, non-standard characters
117 like &eacute; and &aelig;. They can only add 127 characters,
118 so usually only support one script at a time. When you
119 see a page on the web, chances are it's encoded in one
120 of these encodings.</li>
121 <li><strong>Unicode-based encodings</strong> implement the
122 Unicode standard and include UTF-8, UCS-2 and UTF-16.
123 They go beyond 8-bits (the first two are variable length,
124 while the second one uses 16-bits), and support almost
125 every language in the world. UTF-8 is gaining traction
126 as the dominant international encoding of the web.</li>
127 </ul>
128 </blockquote>
130 <p>The first step of our journey is to find out what the encoding of
131 your website is. The most reliable way is to ask your
132 browser:</p>
134 <dl>
135 <dt>Mozilla Firefox</dt>
136 <dd>Tools &gt; Page Info: Encoding</dd>
137 <dt>Internet Explorer</dt>
138 <dd>View &gt; Encoding: bulleted item is unofficial name</dd>
139 </dl>
141 <p>Internet Explorer won't give you the mime (i.e. useful/real) name of the
142 character encoding, so you'll have to look it up using their description.
143 Some common ones:</p>
145 <table class="table">
146 <thead><tr>
147 <th>IE's Description</th>
148 <th>Mime Name</th>
149 </tr></thead>
150 <tbody>
151 <tr><th colspan="2">Windows</th></tr>
152 <tr><td>Arabic (Windows)</td><td>Windows-1256</td></tr>
153 <tr><td>Baltic (Windows)</td><td>Windows-1257</td></tr>
154 <tr><td>Central European (Windows)</td><td>Windows-1250</td></tr>
155 <tr><td>Cyrillic (Windows)</td><td>Windows-1251</td></tr>
156 <tr><td>Greek (Windows)</td><td>Windows-1253</td></tr>
157 <tr><td>Hebrew (Windows)</td><td>Windows-1255</td></tr>
158 <tr><td>Thai (Windows)</td><td>TIS-620</td></tr>
159 <tr><td>Turkish (Windows)</td><td>Windows-1254</td></tr>
160 <tr><td>Vietnamese (Windows)</td><td>Windows-1258</td></tr>
161 <tr><td>Western European (Windows)</td><td>Windows-1252</td></tr>
162 </tbody>
163 <tbody>
164 <tr><th colspan="2">ISO</th></tr>
165 <tr><td>Arabic (ISO)</td><td>ISO-8859-6</td></tr>
166 <tr><td>Baltic (ISO)</td><td>ISO-8859-4</td></tr>
167 <tr><td>Central European (ISO)</td><td>ISO-8859-2</td></tr>
168 <tr><td>Cyrillic (ISO)</td><td>ISO-8859-5</td></tr>
169 <tr class="minor"><td>Estonian (ISO)</td><td>ISO-8859-13</td></tr>
170 <tr class="minor"><td>Greek (ISO)</td><td>ISO-8859-7</td></tr>
171 <tr><td>Hebrew (ISO-Logical)</td><td>ISO-8859-8-l</td></tr>
172 <tr><td>Hebrew (ISO-Visual)</td><td>ISO-8859-8</td></tr>
173 <tr class="minor"><td>Latin 9 (ISO)</td><td>ISO-8859-15</td></tr>
174 <tr class="minor"><td>Turkish (ISO)</td><td>ISO-8859-9</td></tr>
175 <tr><td>Western European (ISO)</td><td>ISO-8859-1</td></tr>
176 </tbody>
177 <tbody>
178 <tr><th colspan="2">Other</th></tr>
179 <tr><td>Chinese Simplified (GB18030)</td><td>GB18030</td></tr>
180 <tr><td>Chinese Simplified (GB2312)</td><td>GB2312</td></tr>
181 <tr><td>Chinese Simplified (HZ)</td><td>HZ</td></tr>
182 <tr><td>Chinese Traditional (Big5)</td><td>Big5</td></tr>
183 <tr><td>Japanese (Shift-JIS)</td><td>Shift_JIS</td></tr>
184 <tr><td>Japanese (EUC)</td><td>EUC-JP</td></tr>
185 <tr><td>Korean</td><td>EUC-KR</td></tr>
186 <tr><td>Unicode (UTF-8)</td><td>UTF-8</td></tr>
187 </tbody>
188 </table>
190 <p>Internet Explorer does not recognize some of the more obscure
191 character encodings, and having to lookup the real names with a table
192 is a pain, so I recommend using Mozilla Firefox to find out your
193 character encoding.</p>
195 <h2 id="findmetacharset">Finding the embedded encoding</h2>
197 <p>At this point, you may be asking, &quot;Didn't we already find out our
198 encoding?&quot; Well, as it turns out, there are multiple places where
199 a web developer can specify a character encoding, and one such place
200 is in a <code>META</code> tag:</p>
202 <pre>&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot; /&gt;</pre>
204 <p>You'll find this in the <code>HEAD</code> section of an HTML document.
205 The text to the right of <code>charset=</code> is the &quot;claimed&quot;
206 encoding: the HTML claims to be this encoding, but whether or not this
207 is actually the case depends on other factors. For now, take note
208 if your <code>META</code> tag claims that either:</p>
210 <ol>
211 <li>The character encoding is the same as the one reported by the
212 browser,</li>
213 <li>The character encoding is different from the browser's, or</li>
214 <li>There is no <code>META</code> tag at all! (horror, horror!)</li>
215 </ol>
217 <h2 id="fixcharset">Fixing the encoding</h2>
219 <p>If your <code>META</code> encoding and your real encoding match,
220 savvy! You can skip this section. If they don't...</p>
222 <h3 id="fixcharset-none">No embedded encoding</h3>
224 <p>If this is the case, you'll want to add in the appropriate
225 <code>META</code> tag to your website. It's as simple as copy-pasting
226 the code snippet above and replacing UTF-8 with whatever is the mime name
227 of your real encoding.</p>
229 <blockquote class="aside">
230 <p>For all those skeptics out there, there is a very good reason
231 why the character encoding should be explicitly stated. When the
232 browser isn't told what the character encoding of a text is, it
233 has to guess: and sometimes the guess is wrong. Hackers can manipulate
234 this guess in order to slip XSS pass filters and then fool the
235 browser into executing it as active code. A great example of this
236 is the <a href="http://shiflett.org/archive/177">Google UTF-7
237 exploit</a>.</p>
238 <p>You might be able to get away with not specifying a character
239 encoding with the <code>META</code> tag as long as your webserver
240 sends the right Content-Type header, but why risk it? Besides, if
241 the user downloads the HTML file, there is no longer any webserver
242 to define the character encoding.</p>
243 </blockquote>
245 <h3 id="fixcharset-diff">Embedded encoding disagrees</h3>
247 <p>This is an extremely common mistake: another source is telling
248 the browser what the
249 character encoding is and is overriding the embedded encoding. This
250 source usually is the Content-Type HTTP header that the webserver (i.e.
251 Apache) sends. A usual Content-Type header sent with a page might
252 look like this:</p>
254 <pre>Content-Type: text/html; charset=ISO-8859-1</pre>
256 <p>Notice how there is a charset parameter: this is the webserver's
257 way of telling a browser what the character encoding is, much like
258 the <code>META</code> tags we touched upon previously.</p>
260 <blockquote class="aside"><p>In fact, the <code>META</code> tag is
261 designed as a substitute for the HTTP header for contexts where
262 sending headers is impossible (such as locally stored files without
263 a webserver). Thus the name <code>http-equiv</code> (HTTP equivalent).
264 </p></blockquote>
266 <p>There are two ways to go about fixing this: changing the <code>META</code>
267 tag to match the HTTP header, or changing the HTTP header to match
268 the <code>META</code> tag. How do we know which to do? It depends
269 on the website's content: after all, headers and tags are only ways of
270 describing the actual characters on the web page.</p>
272 <p>If your website:</p>
274 <dl>
275 <dt>...only uses ASCII characters,</dt>
276 <dd>Either way is fine, but I recommend switching both to
277 UTF-8 (more on this later).</dd>
278 <dt>...uses special characters, and they display
279 properly,</dt>
280 <dd>Change the embedded encoding to the server encoding.</dd>
281 <dt>...uses special characters, but users often complain that
282 they come out garbled,</dt>
283 <dd>Change the server encoding to the embedded encoding.</dd>
284 </dl>
286 <p>Changing a META tag is easy: just swap out the old encoding
287 for the new. Changing the server (HTTP header) encoding, however,
288 is slightly more difficult.</p>
290 <h3 id="fixcharset-server">Changing the server encoding</h3>
292 <h4 id="fixcharset-server-php">PHP header() function</h4>
294 <p>The simplest way to handle this problem is to send the encoding
295 yourself, via your programming language. Since you're using HTML
296 Purifier, I'll assume PHP, although it's not too difficult to do
297 similar things in
298 <a href="http://www.w3.org/International/O-HTTP-charset#scripting">other
299 languages</a>. The appropriate code is:</p>
301 <pre><a href="http://php.net/function.header">header</a>('Content-Type:text/html; charset=UTF-8');</pre>
303 <p>...replacing UTF-8 with whatever your embedded encoding is.
304 This code must come before any output, so be careful about
305 stray whitespace in your application.</p>
307 <h4 id="fixcharset-server-phpini">PHP ini directive</h4>
309 <p>PHP also has a neat little ini directive that can save you a
310 header call: <code><a href="http://php.net/ini.core#ini.default-charset">default_charset</a></code>. Using this code:</p>
312 <pre><a href="http://php.net/function.ini_set">ini_set</a>('default_charset', 'UTF-8');</pre>
314 <p>...will also do the trick. If PHP is running as an Apache module (and
315 not as FastCGI, consult
316 <a href="http://php.net/phpinfo">phpinfo</a>() for details), you can even use htaccess do apply this property
317 globally:</p>
319 <pre><a href="http://php.net/configuration.changes#configuration.changes.apache">php_value</a> default_charset &quot;UTF-8&quot;</pre>
321 <blockquote class="aside"><p>As with all INI directives, this can
322 also go in your php.ini file. Some hosting providers allow you to customize
323 your own php.ini file, ask your support for details. Use:</p>
324 <pre>default_charset = &quot;utf-8&quot;</pre></blockquote>
326 <h4 id="fixcharset-server-nophp">Non-PHP</h4>
328 <p>You may, for whatever reason, need to set the character encoding
329 on non-PHP files, usually plain ol' HTML files. Doing this
330 is more of a hit-or-miss process: depending on the software being
331 used as a webserver and the configuration of that software, certain
332 techniques may work, or may not work.</p>
334 <h4 id="fixcharset-server-htaccess">.htaccess</h4>
336 <p>On Apache, you can use an .htaccess file to change the character
337 encoding. I'll defer to
338 <a href="http://www.w3.org/International/questions/qa-htaccess-charset">W3C</a>
339 for the in-depth explanation, but it boils down to creating a file
340 named .htaccess with the contents:</p>
342 <pre><a href="http://httpd.apache.org/docs/1.3/mod/mod_mime.html#addcharset">AddCharset</a> UTF-8 .html</pre>
344 <p>Where UTF-8 is replaced with the character encoding you want to
345 use and .html is a file extension that this will be applied to. This
346 character encoding will then be set for any file directly in
347 or in the subdirectories of directory you place this file in.</p>
349 <p>If you're feeling particularly courageous, you can use:</p>
351 <pre><a href="http://httpd.apache.org/docs/1.3/mod/core.html#adddefaultcharset">AddDefaultCharset</a> UTF-8</pre>
353 <p>...which changes the character set Apache adds to any document that
354 doesn't have any Content-Type parameters. This directive, which the
355 default configuration file sets to iso-8859-1 for security
356 reasons, is probably why your headers mismatch
357 with the <code>META</code> tag. If you would prefer Apache not to be
358 butting in on your character encodings, you can tell it not
359 to send anything at all:</p>
361 <pre><a href="http://httpd.apache.org/docs/1.3/mod/core.html#adddefaultcharset">AddDefaultCharset</a> Off</pre>
363 <p>...making your <code>META</code> tags the sole source of
364 character encoding information. In these cases, it is
365 <em>especially</em> important to make sure you have valid <code>META</code>
366 tags on your pages and all the text before them is ASCII.</p>
368 <blockquote class="aside"><p>These directives can also be
369 placed in httpd.conf file for Apache, but
370 in most shared hosting situations you won't be able to edit this file.
371 </p></blockquote>
373 <h4 id="fixcharset-server-ext">File extensions</h4>
375 <p>If you're not allowed to use .htaccess files, you can often
376 piggy-back off of Apache's default AddCharset declarations to get
377 your files in the proper extension. Here are Apache's default
378 character set declarations:</p>
380 <table class="table">
381 <thead><tr>
382 <th>Charset</th>
383 <th>File extension(s)</th>
384 </tr></thead>
385 <tbody>
386 <tr><td>ISO-8859-1</td><td>.iso8859-1 .latin1</td></tr>
387 <tr><td>ISO-8859-2</td><td>.iso8859-2 .latin2 .cen</td></tr>
388 <tr><td>ISO-8859-3</td><td>.iso8859-3 .latin3</td></tr>
389 <tr><td>ISO-8859-4</td><td>.iso8859-4 .latin4</td></tr>
390 <tr><td>ISO-8859-5</td><td>.iso8859-5 .latin5 .cyr .iso-ru</td></tr>
391 <tr><td>ISO-8859-6</td><td>.iso8859-6 .latin6 .arb</td></tr>
392 <tr><td>ISO-8859-7</td><td>.iso8859-7 .latin7 .grk</td></tr>
393 <tr><td>ISO-8859-8</td><td>.iso8859-8 .latin8 .heb</td></tr>
394 <tr><td>ISO-8859-9</td><td>.iso8859-9 .latin9 .trk</td></tr>
395 <tr><td>ISO-2022-JP</td><td>.iso2022-jp .jis</td></tr>
396 <tr><td>ISO-2022-KR</td><td>.iso2022-kr .kis</td></tr>
397 <tr><td>ISO-2022-CN</td><td>.iso2022-cn .cis</td></tr>
398 <tr><td>Big5</td><td>.Big5 .big5 .b5</td></tr>
399 <tr><td>WINDOWS-1251</td><td>.cp-1251 .win-1251</td></tr>
400 <tr><td>CP866</td><td>.cp866</td></tr>
401 <tr><td>KOI8-r</td><td>.koi8-r .koi8-ru</td></tr>
402 <tr><td>KOI8-ru</td><td>.koi8-uk .ua</td></tr>
403 <tr><td>ISO-10646-UCS-2</td><td>.ucs2</td></tr>
404 <tr><td>ISO-10646-UCS-4</td><td>.ucs4</td></tr>
405 <tr><td>UTF-8</td><td>.utf8</td></tr>
406 <tr><td>GB2312</td><td>.gb2312 .gb </td></tr>
407 <tr><td>utf-7</td><td>.utf7</td></tr>
408 <tr><td>EUC-TW</td><td>.euc-tw</td></tr>
409 <tr><td>EUC-JP</td><td>.euc-jp</td></tr>
410 <tr><td>EUC-KR</td><td>.euc-kr</td></tr>
411 <tr><td>shift_jis</td><td>.sjis</td></tr>
412 </tbody>
413 </table>
415 <p>So, for example, a file named <code>page.utf8.html</code> or
416 <code>page.html.utf8</code> will probably be sent with the UTF-8 charset
417 attached, the difference being that if there is an
418 <code>AddCharset charset .html</code> declaration, it will override
419 the .utf8 extension in <code>page.utf8.html</code> (precedence moves
420 from right to left). By default, Apache has no such declaration.</p>
422 <h4 id="fixcharset-server-iis">Microsoft IIS</h4>
424 <p>If anyone can contribute information on how to configure Microsoft
425 IIS to change character encodings, I'd be grateful.</p>
427 <h3 id="fixcharset-xml">XML</h3>
429 <p><code>META</code> tags are the most common source of embedded
430 encodings, but they can also come from somewhere else: XML
431 processing instructions. They look like:</p>
433 <pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</pre>
435 <p>...and are most often found in XML documents (including XHTML).</p>
437 <p>For XHTML, this processing instruction theoretically
438 overrides the <code>META</code> tag. In reality, this happens only when the
439 XHTML is actually served as legit XML and not HTML, which is almost always
440 never due to Internet Explorer's lack of support for
441 <code>application/xhtml+xml</code> (even though doing so is often
442 argued to be <a href="http://www.hixie.ch/advocacy/xhtml">good practice</a>).</p>
444 <p>For XML, however, this processing instruction is extremely important.
445 Since most webservers are not configured to send charsets for .xml files,
446 this is the only thing a parser has to go on. Furthermore, the default
447 for XML files is UTF-8, which often butts heads with more common
448 ISO-8859-1 encoding (you see this in garbled RSS feeds).</p>
450 <p>In short, if you use XHTML and have gone through the
451 trouble of adding the XML header, make sure it jives
452 with your <code>META</code> tags and HTTP headers.</p>
454 <h3 id="fixcharset-internals">Inside the process</h3>
456 <p>This section is not required reading,
457 but may answer some of your questions on what's going on in all
458 this character encoding hocus pocus. If you're interested in
459 moving on to the next phase, skip this section.</p>
461 <p>A logical question that follows all of our wheeling and dealing
462 with multiple sources of character encodings is &quot;Why are there
463 so many options?&quot; To answer this question, we have to turn
464 back our definition of character encodings: they allow a program
465 to interpret bytes into human-readable characters.</p>
467 <p>Thus, a chicken-egg problem: a character encoding
468 is necessary to interpret the
469 text of a document. A <code>META</code> tag is in the text of a document.
470 The <code>META</code> tag gives the character encoding. How can we
471 determine the contents of a <code>META</code> tag, inside the text,
472 if we don't know it's character encoding? And how do we figure out
473 the character encoding, if we don't know the contents of the
474 <code>META</code> tag?</p>
476 <p>Fortunantely for us, the characters we need to write the
477 <code>META</code> are in ASCII, which is pretty much universal
478 over every character encoding that is in common use today. So,
479 all the web-browser has to do is parse all the way down until
480 it gets to the Content-Type tag, extract the character encoding
481 tag, then re-parse the document according to this new information.</p>
483 <p>Obviously this is complicated, so browsers prefer the simpler
484 and more efficient solution: get the character encoding from a
485 somewhere other than the document itself, i.e. the HTTP headers,
486 much to the chagrin of HTML authors who can't set these headers.</p>
488 <h2 id="whyutf8">Why UTF-8?</h2>
490 <p>So, you've gone through all the trouble of ensuring that your
491 server and embedded characters all line up properly and are
492 present. Good job: at
493 this point, you could quit and rest easy knowing that your pages
494 are not vulnerable to character encoding style XSS attacks.
495 However, just as having a character encoding is better than
496 having no character encoding at all, having UTF-8 as your
497 character encoding is better than having some other random
498 character encoding, and the next step is to convert to UTF-8.
499 But why?</p>
501 <h3 id="whyutf8-i18n">Internationalization</h3>
503 <p>Many software projects, at one point or another, suddenly realize
504 that they should be supporting more than one language. Even regular
505 usage in one language sometimes requires the occasional special character
506 that, without surprise, is not available in your character set. Sometimes
507 developers get around this by adding support for multiple encodings: when
508 using Chinese, use Big5, when using Japanese, use Shift-JIS, when
509 using Greek, etc. Other times, they use character entities with great
510 zeal.</p>
512 <p>UTF-8, however, obviates the need for any of these complicated
513 measures. After getting the system to use UTF-8 and adjusting for
514 sources that are outside the hand of the browser (more on this later),
515 UTF-8 just works. You can use it for any language, even many languages
516 at once, you don't have to worry about managing multiple encodings,
517 you don't have to use those user-unfriendly entities.</p>
519 <h3 id="whyutf8-user">User-friendly</h3>
521 <p>Websites encoded in Latin-1 (ISO-8859-1) which ocassionally need
522 a special character outside of their scope often will use a character
523 entity to achieve the desired effect. For instance, &theta; can be
524 written <code>&amp;theta;</code>, regardless of the character encoding's
525 support of Greek letters.</p>
527 <p>This works nicely for limited use of special characters, but
528 say you wanted this sentence of Chinese text: &#28608;&#20809;,
529 &#36889;&#20841;&#20491;&#23383;&#26159;&#29978;&#40636;&#24847;&#24605;.
530 The entity-ized version would look like this:</p>
532 <pre>&amp;#28608;&amp;#20809;, &amp;#36889;&amp;#20841;&amp;#20491;&amp;#23383;&amp;#26159;&amp;#29978;&amp;#40636;&amp;#24847;&amp;#24605;</pre>
534 <p>Extremely inconvenient for those of us who actually know what
535 character entities are, totally unintelligible to poor users who don't!
536 Even the slightly more user-friendly, &quot;intelligible&quot; character
537 entities like <code>&amp;theta;</code> will leave users who are
538 uninterested in learning HTML scratching their heads. On the other
539 hand, if they see &theta; in an edit box, they'll know that it's a
540 special character, and treat it accordingly, even if they don't know
541 how to write that character themselves.</p>
543 <blockquote class="aside"><p>Wikipedia is a great case study for
544 an application that originally used ISO-8859-1 but switched to UTF-8
545 when it became far to cumbersome to support foreign languages. Bots
546 will now actually go through articles and convert character entities
547 to their corresponding real characters for the sake of user-friendliness
548 and searcheability. See
549 <a href="http://meta.wikimedia.org/wiki/Help:Special_characters">Meta's
550 page on special characters</a> for more details.
551 </p></blockquote>
553 <h3 id="whyutf8-forms">Forms</h3>
555 <p>While we're on the tack of users, how do non-UTF-8 web forms deal
556 with characters that our outside of their character set? Rather than
557 discuss what UTF-8 does right, we're going to show what could go wrong
558 if you didn't use UTF-8 and people tried to use characters outside
559 of your character encoding.</p>
561 <p>The troubles are large, extensive, and extremely difficult to fix (or,
562 at least, difficult enough that if you had the time and resources to invest
563 in doing the fix, you would be probably better off migrating to UTF-8).
564 There are two types of form submission: <code>application/x-www-form-urlencoded</code>
565 which is used for GET and by default for POST, and <code>multipart/form-data</code>
566 which may be used by POST, and is required when you want to upload
567 files.</p>
569 <p>The following is a summarization of notes from
570 <a href="http://ppewww.physics.gla.ac.uk/~flavell/charset/form-i18n.html">
571 <code>FORM</code> submission and i18n</a>. That document contains lots
572 of useful information, but is written in a rambly manner, so
573 here I try to get right to the point.</p>
575 <h4 id="whyutf8-forms-urlencoded"><code>application/x-www-form-urlencoded</code></h4>
577 <p>This is the Content-Type that GET requests must use, and POST requests
578 use by default. It involves the ubiquituous percent encoding format that
579 looks something like: <code>%C3%86</code>. There is no official way of
580 determining the character encoding of such a request, since the percent
581 encoding operates on a byte level, so it is usually assumed that it
582 is the same as the encoding the page containing the form was submitted
583 in. You'll run into very few problems if you only use characters in
584 the character encoding you chose.</p>
586 <p>However, once you start adding characters outside of your encoding
587 (and this is a lot more common than you may think: take curly
588 &quot;smart&quot; quotes from Microsoft as an example),
589 a whole manner of strange things start to happen. Depending on the
590 browser you're using, they might:</p>
592 <ul>
593 <li>Replace the unsupported characters with useless question marks,</li>
594 <li>Attempt to fix the characters (example: smart quotes to regular quotes),</li>
595 <li>Replace the character with a character entity, or</li>
596 <li>Send it anyway as a different character encoding mixed in
597 with the original encoding (usually Windows-1252 rather than
598 iso-8859-1 or UTF-8 interspersed in 8-bit)</li>
599 </ul>
601 <p>To properly guard against these behaviors, you'd have to sniff out
602 the browser agent, compile a database of different behaviors, and
603 take appropriate conversion action against the string (disregarding
604 a spate of extremely mysterious, random and devastating bugs Internet
605 Explorer manifests every once in a while). Or you could
606 use UTF-8 and rest easy knowing that none of this could possibly happen
607 since UTF-8 supports every character.</p>
609 <h4 id="whyutf8-forms-multipart"><code>multipart/form-data</code></h4>
611 <p>Multipart form submission takes a way a lot of the ambiguity
612 that percent-encoding had: the server now can explicitly ask for
613 certain encodings, and the client can explicitly tell the server
614 during the form submission what encoding the fields are in.</p>
616 <p>There are two ways you go with this functionality: leave it
617 unset and have the browser send in the same encoding as the page,
618 or set it to UTF-8 and then do another conversion server-side.
619 Each method has deficiencies, especially the former.</p>
621 <p>If you tell the browser to send the form in the same encoding as
622 the page, you still have the trouble of what to do with characters
623 that are outside of the character encoding's range. The behavior, once
624 again, varies: Firefox 2.0 entity-izes them while Internet Explorer
625 7.0 mangles them beyond intelligibility. For serious internationalization purposes,
626 this is not an option.</p>
628 <p>The other possibility is to set Accept-Encoding to UTF-8, which
629 begs the question: Why aren't you using UTF-8 for everything then?
630 This route is more palatable, but there's a notable caveat: your data
631 will come in as UTF-8, so you will have to explicitly convert it into
632 your favored local character encoding.</p>
634 <p>I object to this approach on idealogical grounds: you're
635 digging yourself deeper into
636 the hole when you could have been converting to UTF-8
637 instead. And, of course, you can't use this method for GET requests.</p>
639 <h3 id="whyutf8-support">Well supported</h3>
641 <p>Almost every modern browser in the wild today has full UTF-8 and Unicode
642 support: the number of troublesome cases can be counted with the
643 fingers of one hand, and these browsers usually have trouble with
644 other character encodings too. Problems users usually encounter stem
645 from the lack of appropriate fonts to display the characters (once
646 again, this applies to all character encodings and HTML entities) or
647 Internet Explorer's lack of intelligent font picking (which can be
648 worked around).</p>
650 <p>We will go into more detail about how to deal with edge cases in
651 the browser world in the Migration section, but rest assured that
652 converting to UTF-8, if done correctly, will not result in users
653 hounding you about broken pages.</p>
655 <h3 id="whyutf8-htmlpurifier">HTML Purifier</h3>
657 <p>And finally, we get to HTML Purifier. HTML Purifier is built to
658 deal with UTF-8: any indications otherwise are the result of an
659 encoder that converts text from your preferred encoding to UTF-8, and
660 back again. HTML Purifier never touches anything else, and leaves
661 it up to the module iconv to do the dirty work.</p>
663 <p>This approach, however, is not perfect. iconv is blithely unaware
664 of HTML character entities. HTML Purifier, in order to
665 protect against sophisticated escaping schemes, normalizes all character
666 and numeric entities before processing the text. This leads to
667 one important ramification:</p>
669 <p><strong>Any character that is not supported by the target character
670 set, regardless of whether or not it is in the form of a character
671 entity or a raw character, will be silently ignored.</strong></p>
673 <p>Example of this principle at work: say you have <code>&amp;theta;</code>
674 in your HTML, but the output is in Latin-1 (which, understandably,
675 does not understand Greek), the following process will occur (assuming you've
676 set the encoding correctly using %Core.Encoding):</p>
678 <ul>
679 <li>The <code>Encoder</code> will transform the text from ISO 8859-1 to UTF-8
680 (note that theta is preserved since it doesn't actually use
681 any non-ASCII characters): <code>&amp;theta;</code></li>
682 <li>The <code>EntityParser</code> will transform all named and numeric
683 character entities to their corresponding raw UTF-8 equivalents:
684 <code>&theta;</code></li>
685 <li>HTML Purifier processes the code: <code>&theta;</code></li>
686 <li>The <code>Encoder</code> now transforms the text back from UTF-8
687 to ISO 8859-1. Since Greek is not supported by ISO 8859-1, it
688 will be either ignored or replaced with a question mark:
689 <code>?</code></li>
690 </ul>
692 <p>This behaviour is quite unsatisfactory. It is a deal-breaker for
693 international applications, and it can be mildly annoying for the provincial
694 soul who occasionally needs a special character. Since 1.4.0, HTML
695 Purifier has provided a slightly more palatable workaround using
696 %Core.EscapeNonASCIICharacters. The process now looks like:</p>
698 <ul>
699 <li>The <code>Encoder</code> transforms encoding to UTF-8: <code>&amp;theta;</code></li>
700 <li>The <code>EntityParser</code> transforms entities: <code>&theta;</code></li>
701 <li>HTML Purifier processes the code: <code>&theta;</code></li>
702 <li>The <code>Encoder</code> replaces all non-ASCII characters
703 with numeric entities: <code>&amp;#952;</code></li>
704 <li>For good measure, <code>Encoder</code> transforms encoding back to
705 original (which is strictly unnecessary for 99% of encodings
706 out there): <code>&amp;#952;</code> (remember, it's all ASCII!)</li>
707 </ul>
709 <p>...which means that this is only good for an occasional foray into
710 the land of Unicode characters, and is totally unacceptable for Chinese
711 or Japanese texts. The even bigger kicker is that, supposing the
712 input encoding was actually ISO-8859-7, which <em>does</em> support
713 theta, the character would get entity-ized anyway! (The Encoder does
714 not discriminate).</p>
716 <p>The current functionality is about where HTML Purifier will be for
717 the rest of eternity. HTML Purifier could attempt to preserve the original
718 form of the entities so that they could be substituted back in, only the
719 DOM extension kills them off irreversibly. HTML Purifier could also attempt
720 to be smart and only convert non-ASCII characters that weren't supported
721 by the target encoding, but that would require reimplementing iconv
722 with HTML awareness, something I will not do.</p>
724 <p>So there: either it's UTF-8 or crippled international support. Your pick! (and I'm
725 not being sarcastic here: some people could care less about other languages)</p>
727 <h2 id="migrate">Migrate to UTF-8</h2>
729 <p>So, you've decided to bite the bullet, and want to migrate to UTF-8.
730 Note that this is not for the faint-hearted, and you should expect
731 the process to take longer than you think it will take.</p>
733 <p>The general idea is that you convert all existing text to UTF-8,
734 and then you set all the headers and META tags we discussed earlier
735 to UTF-8. There are many ways going about doing this: you could
736 write a conversion script that runs through the database and re-encodes
737 everything as UTF-8 or you could do the conversion on the fly when someone
738 reads the page. The details depend on your system, but I will cover
739 some of the more subtle points of migration that may trip you up.</p>
741 <h3 id="migrate-db">Configuring your database</h3>
743 <p>Most modern databases, the most prominent open-source ones being MySQL
744 4.1+ and PostgreSQL, support character encodings. If you're switching
745 to UTF-8, logically speaking, you'd want to make sure your database
746 knows about the change too. There are some caveats though:</p>
748 <h4 id="migrate-db-legit">Legit method</h4>
750 <p>Standardization in terms of SQL syntax for specifying character
751 encodings is notoriously spotty. Refer to your respective database's
752 documentation on how to do this properly.</p>
754 <p>For <a href="http://dev.mysql.com/doc/refman/5.0/en/charset-conversion.html">MySQL</a>, <code>ALTER</code> will magically perform the
755 character encoding conversion for you. However, you have
756 to make sure that the text inside the column is what is says it is:
757 if you had put Shift-JIS in an ISO 8859-1 column, MySQL will irreversibly mangle
758 the text when you try to convert it to UTF-8. You'll have to convert
759 it to a binary field, convert it to a Shift-JIS field (the real encoding),
760 and then finally to UTF-8. Many a website had pages irreversibly mangled
761 because they didn't realize that they'd been deluding themselves about
762 the character encoding all along, don't become the next victim.</p>
764 <p>For <a href="http://www.postgresql.org/docs/8.2/static/multibyte.html">PostgreSQL</a>, there appears to be no direct way to change the
765 encoding of a database (as of 8.2). You will have to dump the data, and then reimport
766 it into a new table. Make sure that your client encoding is set properly:
767 this is how PostgreSQL knows to perform an encoding conversion.</p>
769 <p>Many times, you will be also asked about the &quot;collation&quot; of
770 the new column. Collation is how a DBMS sorts text, like ordering
771 B, C and A into A, B and C (the problem gets surprisingly complicated
772 when you get to languages like Thai and Japanese). If in doubt,
773 going with the default setting is usually a safe bet.</p>
775 <p>Once the conversion is all said and done, you still have to remember
776 to set the client encoding (your encoding) properly on each database
777 connection using <code>SET NAMES</code> (which is standard SQL and is
778 usually supported).</p>
780 <h4 id="migrate-db-binary">Binary</h4>
782 <p>Due to the abovementioned compatibility issues, a more interoperable
783 way of storing UTF-8 text is to stuff it in a binary datatype.
784 <code>CHAR</code> becomes <code>BINARY</code>, <code>VARCHAR</code> becomes
785 <code>VARBINARY</code> and <code>TEXT</code> becomes <code>BLOB</code>.
786 Doing so can save you some huge headaches:</p>
788 <ul>
789 <li>The syntax for binary data types is very portable,</li>
790 <li>MySQL 4.0 has <em>no</em> support for character encodings, so
791 if you want to support it you <em>have</em> to use binary,</li>
792 <li>MySQL, as of 5.1, has no support for four byte UTF-8 characters,
793 which represent characters beyond the basic multilingual
794 plane, and</li>
795 <li>You will never have to worry about your DBMS being too smart
796 and attempting to convert your text when you don't want it to.</li>
797 </ul>
799 <p>MediaWiki, a very prominent international application, uses binary fields
800 for storing their data because of point three.</p>
802 <p>There are drawbacks, of course:</p>
804 <ul>
805 <li>Database tools like PHPMyAdmin won't be able to offer you inline
806 text editing, since it is declared as binary,</li>
807 <li>It's not semantically correct: it's really text not binary
808 (lying to the database),</li>
809 <li>Unless you use the not-very-portable wizardry mentioned above,
810 you have to change the encoding yourself (usually, you'd do
811 it on the fly), and</li>
812 <li>You will not have collation.</li>
813 </ul>
815 <p>Choose based on your circumstances.</p>
817 <h3 id="migrate-editor">Text editor</h3>
819 <p>For more flat-file oriented systems, you will often be tasked with
820 converting reams of existing text and HTML files into UTF-8, as well as
821 making sure that all new files uploaded are properly encoded. Once again,
822 I can only point vaguely in the right direction for converting your
823 existing files: make sure you backup, make sure you use
824 <a href="http://php.net/ref.iconv">iconv</a>(), and
825 make sure you know what the original character encoding of the files
826 is (or are, depending on the tidiness of your system).</p>
828 <p>However, I can proffer more specific advice on the subject of
829 text editors. Many text editors have notoriously spotty Unicode support.
830 To find out how your editor is doing, you can check out <a
831 href="http://www.alanwood.net/unicode/utilities_editors.html">this list</a>
832 or <a href="http://en.wikipedia.org/wiki/Comparison_of_text_editors#Encoding_support">Wikipedia's list.</a>
833 I personally use Notepad++, which works like a charm when it comes to UTF-8.
834 Usually, you will have to <strong>explicitly</strong> tell the editor through some dialogue
835 (usually Save as or Format) what encoding you want it to use. An editor
836 will often offer &quot;Unicode&quot; as a method of saving, which is
837 ambiguous. Make sure you know whether or not they really mean UTF-8
838 or UTF-16 (which is another flavor of Unicode).</p>
840 <p>The two things to look out for are whether or not the editor
841 supports <strong>font mixing</strong> (multiple
842 fonts in one document) and whether or not it adds a <strong>BOM</strong>.
843 Font mixing is important because fonts rarely have support for every
844 language known to mankind: in order to be flexible, an editor must
845 be able to take a little from here and a little from there, otherwise
846 all your Chinese characters will come as nice boxes. We'll discuss
847 BOM below.</p>
849 <h3 id="migrate-bom">Byte Order Mark (headers already sent!)</h3>
851 <p>The BOM, or <a href="http://en.wikipedia.org/wiki/Byte_Order_Mark">Byte
852 Order Mark</a>, is a magical, invisible character placed at
853 the beginning of UTF-8 files to tell people what the encoding is and
854 what the endianness of the text is. It is also unnecessary.</p>
856 <p>Because it's invisible, it often
857 catches people by surprise when it starts doing things it shouldn't
858 be doing. For example, this PHP file:</p>
860 <pre><strong>BOM</strong>&lt;?php
861 header('Location: index.php');
862 ?&gt;</pre>
864 <p>...will fail with the all too familiar <strong>Headers already sent</strong>
865 PHP error. And because the BOM is invisible, this culprit will go unnoticed.
866 My suggestion is to only use ASCII in PHP pages, but if you must, make
867 sure the page is saved WITHOUT the BOM.</p>
869 <blockquote class="aside">
870 <p>The headers the error is referring to are <strong>HTTP headers</strong>,
871 which are sent to the browser before any HTML to tell it various
872 information. The moment any regular text (and yes, a BOM counts as
873 ordinary text) is output, the headers must be sent, and you are
874 not allowed to send anymore. Thus, the error.</p>
875 </blockquote>
877 <p>If you are reading in text files to insert into the middle of another
878 page, it is strongly advised (but not strictly necessary) that you replace out the UTF-8 byte
879 sequence for BOM <code>&quot;\xEF\xBB\xBF&quot;</code> before inserting it in,
880 via:</p>
882 <pre>$text = str_replace(&quot;\xEF\xBB\xBF&quot;, '', $text);</pre>
884 <h3 id="migrate-fonts">Fonts</h3>
886 <p>Generally speaking, people who are having trouble with fonts fall
887 into two categories:</p>
889 <ul>
890 <li>Those who want to
891 use an extremely obscure language for which there is very little
892 support even among native speakers of the language, and</li>
893 <li>Those where the primary language of the text is
894 well-supported but there are occasional characters
895 that aren't supported.</li>
896 </ul>
898 <p>Yes, there's always a chance where an English user happens across
899 a Sinhalese website and doesn't have the right font. But an English user
900 who happens not to have the right fonts probably has no business reading Sinhalese
901 anyway. So we'll deal with the other two edge cases.</p>
903 <h4 id="migrate-fonts-obscure">Obscure scripts</h4>
905 <p>If you run a Bengali website, you may get comments from users who
906 would like to read your website but get heaps of question marks or
907 other meaningless characters. Fixing this problem requires the
908 installation of a font or language pack which is often highly
909 dependent on what the language is. <a href="http://bn.wikipedia.org/wiki/%E0%A6%89%E0%A6%87%E0%A6%95%E0%A6%BF%E0%A6%AA%E0%A7%87%E0%A6%A1%E0%A6%BF%E0%A6%AF%E0%A6%BC%E0%A6%BE:Bangla_script_display_help">Here is an example</a>
910 of such a help file for the Bengali language, I am sure there are
911 others out there too. You just have to point users to the appropriate
912 help file.</p>
914 <h4 id="migrate-fonts-occasional">Occasional use</h4>
916 <p>A prime example of when you'll see some very obscure Unicode
917 characters embedded in what otherwise would be very bland ASCII are
918 letters of the
919 <a href="http://en.wikipedia.org/wiki/International_Phonetic_Alphabet">International
920 Phonetic Alphabet (IPA)</a>, use to designate pronounciations in a very standard
921 manner (you probably see them all the time in your dictionary). Your
922 average font probably won't have support for all of the IPA characters
923 like &#664; (bilabial click) or &#658; (voiced postalveolar fricative).
924 So what's a poor browser to do? Font mix! Smart browsers like Mozilla Firefox
925 and Internet Explorer 7 will borrow glyphs from other fonts in order
926 to make sure that all the characters display properly.</p>
928 <p>But what happens when the browser isn't smart and happens to be the
929 most widely used browser in the entire world? Microsoft IE 6
930 is not smart enough to borrow from other fonts when a character isn't
931 present, so more often than not you'll be slapped with a nice big &#65533;.
932 To get things to work, MSIE 6 needs a little nudge. You could configure it
933 to use a different font to render the text, but you can acheive the same
934 effect by selectively changing the font for blocks of special characters
935 to known good Unicode fonts.</p>
937 <p>Fortunantely, the folks over at Wikipedia have already done all the
938 heavy lifting for you. Get the CSS from the horses mouth here:
939 <a href="http://en.wikipedia.org/wiki/MediaWiki:Common.css">Common.css</a>,
940 and search for &quot;.IPA&quot; There are also a smattering of
941 other classes you can use for other purposes, check out
942 <a href="http://meta.wikimedia.org/wiki/Help:Special_characters#Displaying_Special_Characters">this page</a>
943 for more details. For you lazy ones, this should work:</p>
945 <pre>.Unicode {
946 font-family: Code2000, &quot;TITUS Cyberbit Basic&quot;, &quot;Doulos SIL&quot;,
947 &quot;Chrysanthi Unicode&quot;, &quot;Bitstream Cyberbit&quot;,
948 &quot;Bitstream CyberBase&quot;, Thryomanes, Gentium, GentiumAlt,
949 &quot;Lucida Grande&quot;, &quot;Arial Unicode MS&quot;, &quot;Microsoft Sans Serif&quot;,
950 &quot;Lucida Sans Unicode&quot;;
951 font-family /**/:inherit; /* resets fonts for everyone but IE6 */
952 }</pre>
954 <p>The standard usage goes along the lines of <code>&lt;span class=&quot;Unicode&quot;&gt;Crazy
955 Unicode stuff here&lt;/span&gt;</code>. Characters in the
956 <a href="http://en.wikipedia.org/wiki/Windows_Glyph_List_4">Windows Glyph List</a>
957 usually don't need to be fixed, but for anything else you probably
958 want to play it safe. Unless, of course, you don't care about IE6
959 users.</p>
961 <h3 id="migrate-variablewidth">Dealing with variable width in functions</h3>
963 <p>When people claim that PHP6 will solve all our Unicode problems, they're
964 misinformed. It will not fix any of the abovementioned troubles. It will,
965 however, fix the problem we are about to discuss: processing UTF-8 text
966 in PHP.</p>
968 <p>PHP (as of PHP5) is blithely unaware of the existence of UTF-8 (with a few
969 notable exceptions). Sometimes, this will cause problems, other times,
970 this won't. So far, we've avoided discussing the architecture of
971 UTF-8, so, we must first ask, what is UTF-8? Yes, it supports Unicode,
972 and yes, it is variable width. Other traits:</p>
974 <ul>
975 <li>Every character's byte sequence is unique and will never be found
976 inside the byte sequence of another character,</li>
977 <li>UTF-8 may use up to four bytes to encode a character,</li>
978 <li>UTF-8 text must be checked for well-formedness,</li>
979 <li>Pure ASCII is also valid UTF-8, and</li>
980 <li>Binary sorting will sort UTF-8 in the same order as Unicode.</li>
981 </ul>
983 <p>Each of these traits affect different domains of text processing
984 in different ways. It is beyond the scope of this document to explain
985 what precisely these implications are. PHPWact provides
986 a very good <a href="http://www.phpwact.org/php/i18n/utf-8">reference document</a>
987 on what to expect from each functions, although coverage is spotty in
988 some areas. Their more general notes on
989 <a href="http://www.phpwact.org/php/i18n/charsets">character sets</a>
990 are also worth looking at for information on UTF-8. Some rules of thumb
991 when dealing with Unicode text:</p>
993 <ul>
994 <li>Do not EVER use functions that:<ul>
995 <li>...convert case (strtolower, strtoupper, ucfirst, ucwords)</li>
996 <li>...claim to be case-insensitive (str_ireplace, stristr, strcasecmp)</li>
997 </ul></li>
998 <li>Think twice before using functions that:<ul>
999 <li>...count characters (strlen will return bytes, not characters;
1000 str_split and word_wrap may corrupt)</li>
1001 <li>...entity-ize things (UTF-8 doesn't need entities)</li>
1002 <li>...do very complex string processing (*printf)</li>
1003 </ul></li>
1004 </ul>
1006 <p>Note: this list applies to UTF-8 encoded text only: if you have
1007 a string that you are 100% sure is ASCII, be my guest and use
1008 <code>strtolower</code> (HTML Purifier uses this function.)</p>
1010 <p>Regardless, always think in bytes, not characters. If you use strpos()
1011 to find the position of a character, it will be in bytes, but this
1012 usually won't matter since substr() also operates with byte indices!</p>
1014 <p>You'll also need to make sure your UTF-8 is well-formed and will
1015 probably need replacements for some of these functions. I recommend
1016 using Harry Fuecks' <a href="http://phputf8.sourceforge.net/">PHP
1017 UTF-8</a> library, rather than use mb_string directly. HTML Purifier
1018 also defines a few useful UTF-8 compatible functions: check out
1019 <code>Encoder.php</code> in the <code>/library/HTMLPurifier/</code>
1020 directory.</p>
1022 <h2 id="externallinks">Further Reading</h2>
1024 <p>Well, that's it. Hopefully this document has served as a very
1025 practical springboard into knowledge of how UTF-8 works. You may have
1026 decided that you don't want to migrate yet: that's fine, just know
1027 what will happen to your output and what bug reports you may recieve.</p>
1029 <p>Many other developers have already discussed the subject of Unicode,
1030 UTF-8 and internationalization, and I would like to defer to them for
1031 a more in-depth look into character sets and encodings.</p>
1033 <ul>
1034 <li><a href="http://www.joelonsoftware.com/articles/Unicode.html">
1035 The Absolute Minimum Every Software Developer Absolutely,
1036 Positively Must Know About Unicode and Character Sets
1037 (No Excuses!)</a> by Joel Spolsky, provides a <em>very</em>
1038 good high-level look at Unicode and character sets in general.</li>
1039 <li><a href="http://en.wikipedia.org/wiki/UTF-8">UTF-8 on Wikipedia</a>,
1040 provides a lot of useful details into the innards of UTF-8, although
1041 it may be a little off-putting to people who don't know much
1042 about Unicode to begin with.</li>
1043 </ul>
1045 </body>
1046 </html>