Monday, June 15, 2009

php utf-8 chr() and ord() equivalents

As far as I can tell, PHP provides no built in methods of working with multi byte characters.

Here is a simple function for turning an integer value into a character:

function unichr($intval) {
return mb_convert_encoding(pack('n', $intval), 'UTF-8', 'UTF-16BE');
}

A uniord() function can be found in the comments of the PHP manual:
http://us.php.net/manual/en/function.ord.php

3 comments:

Unknown said...

You can generate unicode characters in the following fashion:
"\u2063"

Unknown said...

Why not simply pack('CC', 0x67, 0x62, 0x20, 0x2d, 0x20); or pack('nn', 0x6762, 0x202d); ?

Unknown said...

@Rolf
In PHP, you cannot generate Unicode characters using the "\u" format in PHP. There's no way to directly encode