shared.mb_unicode.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. /**
  3. * Smarty shared plugin
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsShared
  7. */
  8. /**
  9. * convert characters to their decimal unicode equivalents
  10. *
  11. * @link http://www.ibm.com/developerworks/library/os-php-unicode/index.html#listing3 for inspiration
  12. *
  13. * @param string $string characters to calculate unicode of
  14. * @param string $encoding encoding of $string, if null mb_internal_encoding() is used
  15. *
  16. * @return array sequence of unicodes
  17. * @author Rodney Rehm
  18. */
  19. function smarty_mb_to_unicode($string, $encoding = null)
  20. {
  21. if ($encoding) {
  22. $expanded = mb_convert_encoding($string, "UTF-32BE", $encoding);
  23. } else {
  24. $expanded = mb_convert_encoding($string, "UTF-32BE");
  25. }
  26. return unpack("N*", $expanded);
  27. }
  28. /**
  29. * convert unicodes to the character of given encoding
  30. *
  31. * @link http://www.ibm.com/developerworks/library/os-php-unicode/index.html#listing3 for inspiration
  32. *
  33. * @param integer|array $unicode single unicode or list of unicodes to convert
  34. * @param string $encoding encoding of returned string, if null mb_internal_encoding() is used
  35. *
  36. * @return string unicode as character sequence in given $encoding
  37. * @author Rodney Rehm
  38. */
  39. function smarty_mb_from_unicode($unicode, $encoding = null)
  40. {
  41. $t = '';
  42. if (!$encoding) {
  43. $encoding = mb_internal_encoding();
  44. }
  45. foreach ((array) $unicode as $utf32be) {
  46. $character = pack("N*", $utf32be);
  47. $t .= mb_convert_encoding($character, $encoding, "UTF-32BE");
  48. }
  49. return $t;
  50. }