Blaginations

An additional opportunity to be hopelessly wrong

php functions to help you get out of unicode hell.

leave a comment »

function bpad ($instr) {
  return substr("000000", 0, 8 - strlen($instr)) . $instr;
}

function dpad ($instr) {
  return substr("      ", 0, 3 - strlen($instr)) . $instr;
}

function show($instr) {
  for ($i = 0; $i < strlen($instr); $i++) {
    $let = $instr[$i];                                                          
    $no = ord($let);
    echo $i . " : " . $let . " / " . dpad($no) . " / " . bpad(decbin($no)) . " / " . dechex($no).  "\n";
  }
}

Here is an example of use

php > $var = "T" . pack("cc", 195, 171) . "st";
php > show($var);
0 : T /  84 / 01010100 / 54
1 : � / 195 / 11000011 / c3
2 : � / 171 / 10101011 / ab
3 : s / 115 / 01110011 / 73
4 : t / 116 / 01110100 / 74
php > show (utf8_decode($var));
0 : T /  84 / 01010100 / 54
1 : � / 235 / 11101011 / eb
2 : s / 115 / 01110011 / 73
3 : t / 116 / 01110100 / 74

Advertisement

Written by kasterma

September 12, 2011 at 3:30 pm

Posted in Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.