Unichr.t

From TORI
Revision as of 14:17, 21 May 2021 by T (talk | contribs) (Created page with "unichr.t is PHP function that converts the integer number to the Utf8 character and returns this character as string, that contains one, two or three bytes. == Code =...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

unichr.t is PHP function that converts the integer number to the Utf8 character and returns this character as string, that contains one, two or three bytes.

Code

<?php function unichr($dec) {
  if ($dec < 128) {
    $utf = chr($dec);
  } else if ($dec < 2048) {
    $utf = chr(192 + (($dec - ($dec % 64)) / 64));
    $utf .= chr(128 + ($dec % 64));
  } else {
    $utf = chr(224 + (($dec - ($dec % 4096)) / 4096));
    $utf .= chr(128 + ((($dec % 4096) - ($dec % 64)) / 64));
    $utf .= chr(128 + ($dec % 64));
  }
  return $utf;
} 
?>

Example

<?php function unichr($dec) {
  if ($dec < 128) {
    $utf = chr($dec);
  } else if ($dec < 2048) {
    $utf = chr(192 + (($dec - ($dec % 64)) / 64));
    $utf .= chr(128 + ($dec % 64));
  } else {
    $utf = chr(224 + (($dec - ($dec % 4096)) / 4096));
    $utf .= chr(128 + ((($dec % 4096) - ($dec % 64)) / 64));
    $utf .= chr(128 + ($dec % 64));
  }
  return $utf;
} 

echo unichr(98), unichr(12450),"\n";
echo unichr(60),unichr(12367),unichr(12296),"\n";
echo unichr(12296),unichr(12557),unichr(24027),"\n";
echo unichr(12106),unichr(26519),unichr(26862),"\n";
echo unichr(22899),unichr(22907),unichr(23014),"\n";
echo unichr(12525),unichr(26085),unichr(30446),"\n";
?>

Output:

bア
<く〈
〈ㄍ巛
⽊林森
女奻姦
ロ日目

Compare the output with characters at Utf8table.

Analogies

The unichr above is substitute of the "official" PHP routine mb_chr.

the mb_chr is not supported by default in the base setting of PHP. The activation requires certain skills. For many lamers, it is easier to write own code, than to change setting of the PHP. [1]:

function unichr($dec)
{
  if ($dec < 0x80)
  {
    $utf = chr($dec);
  }
  else if ($dec < 0x0800)
  {
    $utf = chr(0xC0 + ($dec >> 6));
    $utf .= chr(0x80 + ($dec & 0x3f));
  }
  else if ($dec < 0x010000)
  {
    $utf = chr(0xE0 + ($dec >> 12));
    $utf .= chr(0x80 + (($dec >> 6) & 0x3f));
    $utf .= chr(0x80 + ($dec & 0x3f));
  }
  else if ($dec < 0x200000)
  {
    $utf = chr(0xF0 + ($dec >> 18));
    $utf .= chr(0x80 + (($dec >> 12) & 0x3f));
    $utf .= chr(0x80 + (($dec >> 6) & 0x3f));
    $utf .= chr(0x80 + ($dec & 0x3f));
  }
  else
  {
    die("UTF-8 character size is more than 4 bytes");
  }
  return $utf;
}

echo unichr(0x263A);
?>

grey - greywyvern - com ¶ 15 years ago (2006) I spent hours looking for a function which would take a numeric HTML entity value and output the appropriate UTF-8 bytes. I found this at another site and only had to modify it slightly; so I don't take credit for this.

<?php function unichr($dec) {
  if ($dec < 128) {
    $utf = chr($dec);
  } else if ($dec < 2048) {
    $utf = chr(192 + (($dec - ($dec % 64)) / 64));
    $utf .= chr(128 + ($dec % 64));
  } else {
    $utf = chr(224 + (($dec - ($dec % 4096)) / 4096));
    $utf .= chr(128 + ((($dec % 4096) - ($dec % 64)) / 64));
    $utf .= chr(128 + ($dec % 64));
  }
  return $utf;
} ?>

References

  1. http://phpweb.hostnet.com.br/manual/fr/function.chr.php chr — Retourne un caractère à partir de son code ASCII. .. 4 vitkorob ¶ (2016)

Keywords

Japanese, Kanji, Mb_str_split.t‎‎ PHP SomeUtf8, SomeH Unicode, Uniord.t Utf8, Utf8table, [[<]], , , , , , , , , , , , , , ,,,,,