Skip to content

Instantly share code, notes, and snippets.

@moustaki
Created October 18, 2010 15:15
Show Gist options
  • Save moustaki/632370 to your computer and use it in GitHub Desktop.
Save moustaki/632370 to your computer and use it in GitHub Desktop.
unicode hex value
<?php
function uniord($c) {
$h = ord($c{0});
if ($h <= 0x7F) {
return $h;
} else if ($h < 0xC2) {
return false;
} else if ($h <= 0xDF) {
return ($h & 0x1F) << 6 | (ord($c{1}) & 0x3F);
} else if ($h <= 0xEF) {
return ($h & 0x0F) << 12 | (ord($c{1}) & 0x3F) << 6
| (ord($c{2}) & 0x3F);
} else if ($h <= 0xF4) {
return ($h & 0x0F) << 18 | (ord($c{1}) & 0x3F) << 12
| (ord($c{2}) & 0x3F) << 6
| (ord($c{3}) & 0x3F);
} else {
return false;
}
}
function hex_format($o) {
$h = strtoupper(dechex($o));
$len = strlen($h);
if ($len % 2 == 1)
$h = "0$h";
return $h;
}
var_dump(hex_format(uniord('€')));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment