PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/functions/ord.php

http://github.com/FSX/php-utf8
PHP | 78 lines | 52 code | 15 blank | 11 comment | 19 complexity | a8cf5d90e958a4e571f79d150229d4ca MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. namespace utf8;
  3. /**
  4. * UTF-8 aware alternative to ord.
  5. *
  6. * Returns the unicode ordinal for a character
  7. *
  8. * @see http://www.php.net/manual/en/function.ord.php#46267
  9. * @param string $chr UTF-8 encoded character
  10. * @return int unicode ordinal for the character
  11. * @package php-utf8
  12. * @subpackage functions
  13. */
  14. function ord($chr)
  15. {
  16. $ord0 = \ord($chr);
  17. if ($ord0 >= 0 && $ord0 <= 127)
  18. return $ord0;
  19. if (!isset($chr[1]))
  20. {
  21. trigger_error('Short sequence - at least 2 bytes expected, only 1 seen');
  22. return false;
  23. }
  24. $ord1 = \ord($chr[1]);
  25. if ($ord0 >= 192 && $ord0 <= 223)
  26. return ($ord0 - 192) * 64 + ($ord1 - 128);
  27. if (!isset($chr[2]))
  28. {
  29. trigger_error('Short sequence - at least 3 bytes expected, only 2 seen');
  30. return false;
  31. }
  32. $ord2 = \ord($chr[2]);
  33. if ($ord0 >= 224 && $ord0 <= 239)
  34. return ($ord0 - 224) * 4096 + ($ord1 - 128) * 64 + ($ord2 - 128);
  35. if (!isset($chr[3]))
  36. {
  37. trigger_error('Short sequence - at least 4 bytes expected, only 3 seen');
  38. return false;
  39. }
  40. $ord3 = \ord($chr[3]);
  41. if ($ord0 >= 240 && $ord0 <= 247)
  42. return ($ord0 - 240) * 262144 + ($ord1 - 128) * 4096 + ($ord2 - 128) * 64 + ($ord3 - 128);
  43. if (!isset($chr[4]))
  44. {
  45. trigger_error('Short sequence - at least 5 bytes expected, only 4 seen');
  46. return false;
  47. }
  48. $ord4 = \ord($chr[4]);
  49. if ($ord0 >= 248 && $ord0 <= 251)
  50. return ($ord0 - 248) * 16777216 + ($ord1 - 128) * 262144 + ($ord2 - 128) * 4096 + ($ord3 - 128) * 64 + ($ord4 - 128);
  51. if (!isset($chr[5]))
  52. {
  53. trigger_error('Short sequence - at least 6 bytes expected, only 5 seen');
  54. return false;
  55. }
  56. if ($ord0 >= 252 && $ord0 <= 253)
  57. return ($ord0 - 252) * 1073741824 + ($ord1 - 128) * 16777216 + ($ord2 - 128) * 262144 + ($ord3 - 128) * 4096 + ($ord4 - 128) * 64 + (\ord($chr[5]) - 128);
  58. if ($ord0 >= 254 && $ord0 <= 255)
  59. {
  60. trigger_error('Invalid UTF-8 with surrogate ordinal '.$ord0);
  61. return false;
  62. }
  63. }