Anyway, my blog now officially supports unicode. That was fun! Or not. My blog is programmed in PHP, and as anyone who's done much PHP knows, nice Unicode handling is a far-off dream in the magical PHP 6. Python handles unicode so much nicer... To convert a code base in PHP to unicode, there are a bunch of functions you have to change and watch out for. Luckily this isn't too tedious as most of them are string functions, and with some clever scripting we can find all the spots in the code that need fixing up. (I also thought about some scripting to do the replacing for me, but I didn't have that many and I like to verify things manually sometimes.)
First up is a helpful guide: http://www.phpwact.org/php/i18n/utf-8 You should definitely skim through that before starting any unicode conversion project, as there are quite a few things you have to worry about. For now, let's get a list of all the string functions we need to change.
Going to the docs, there is a table of contents of all the mb_ multibyte string functions we have to use if we're doing unicode. (Interestingly, str_replace is not among them--it does the right thing automagically. explode is another such function that just works.) One of the things I commonly use a Python REPL for is to format some words I can copy-paste from a website into a programmable form, such as an array of the functions I need. This is really easy stuff (I omit the arrows since I have them off in my REPL):
tab = '''highlight the table of contents functions, copy paste in here
fns = 
for line in tab.split('\n'):
fns.append(line[:line.find('—')].strip()) # is that '—' unicode? Go python!
'(' + '|'.join(fns) + ')'
This gives us the big list in ready-to-regex format:
There's a few more functions though. First, functions such as preg_replace need to have the u attribute specified in the pattern options. So we add to our list:
htmlentities is another gotcha, it really needs the "UTF-8" parameter passed into its third argument. Add that to the list. You should also want to search for htmlspecialchars and html_entity_decode, so add those.
Now take your long string of functions to search for, and paste it onto the command line:
$ find -name '*.php' | xargs grep --color -i -n -E '(fn1|fn2|...|fnN)'
This will print out any occurrences you need. If it looks right, redirect the output to a file for saving. Then you can open it up with vim! (I love vim.) vim will automatically take you to the proper files and lines and you can edit manually to make sure everything is done correctly. (Or you could spend some time coming up with a clever script to do the replacing for you.) The command for vim is:
$ vi -q foundfiles -c copen
After you're done, there is one more detail to take care of. You need to tell PHP to use UTF-8. If you have one index.php file or config file like me that handles everything, this is easy and you place this at the top of that file, otherwise the following needs to be at the top of every executed script.
(You may also need to change your database to work with UTF-8. Most MySQLs do this by default these days.)
Edit: Woops, there was one other thing I forgot to mention. After creating the database connection, you should run:
(If you're not using MySQLi...) A second page of good tips is http://abeautifulsite.net/blog/2010/07/tips-for-supporting-utf-8-in-your-php5-applications/. One of the things my blog has done forever but I also forgot to mention is the HTML settings. You need to add <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> to your header.
Posted on 2011-05-21 by Jach
Trackback URL: https://www.thejach.com/view/2011/5/hello_unicode