shared.mb_unicode.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. * @param string $string characters to calculate unicode of
  13. * @param string $encoding encoding of $string, if null mb_internal_encoding() is used
  14. * @return array sequence of unicodes
  15. * @author Rodney Rehm
  16. */
  17. function smarty_mb_to_unicode($string, $encoding=null) {
  18. if ($encoding) {
  19. $expanded = mb_convert_encoding($string, "UTF-32BE", $encoding);
  20. } else {
  21. $expanded = mb_convert_encoding($string, "UTF-32BE");
  22. }
  23. return unpack("N*", $expanded);
  24. }
  25. /**
  26. * convert unicodes to the character of given encoding
  27. *
  28. * @link http://www.ibm.com/developerworks/library/os-php-unicode/index.html#listing3 for inspiration
  29. * @param integer|array $unicode single unicode or list of unicodes to convert
  30. * @param string $encoding encoding of returned string, if null mb_internal_encoding() is used
  31. * @return string unicode as character sequence in given $encoding
  32. * @author Rodney Rehm
  33. */
  34. function smarty_mb_from_unicode($unicode, $encoding=null) {
  35. $t = '';
  36. if (!$encoding) {
  37. $encoding = mb_internal_encoding();
  38. }
  39. foreach((array) $unicode as $utf32be) {
  40. $character = pack("N*", $utf32be);
  41. $t .= mb_convert_encoding($character, $encoding, "UTF-32BE");
  42. }
  43. return $t;
  44. }
  45. ?>