PageRenderTime 35ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/phputf8/ord.php

https://github.com/dianaprajescu/joomla-framework
PHP | 90 lines | 65 code | 13 blank | 12 comment | 19 complexity | b280b3d72dc6d64e685f70585b5b6dca MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @package utf8
  4. */
  5. //---------------------------------------------------------------
  6. /**
  7. * UTF-8 aware alternative to ord
  8. * Returns the unicode ordinal for a character
  9. * @param string UTF-8 encoded character
  10. * @return int unicode ordinal for the character
  11. * @see http://www.php.net/ord
  12. * @see http://www.php.net/manual/en/function.ord.php#46267
  13. */
  14. function utf8_ord($chr) {
  15. $ord0 = ord($chr);
  16. if ( $ord0 >= 0 && $ord0 <= 127 ) {
  17. return $ord0;
  18. }
  19. if ( !isset($chr{1}) ) {
  20. trigger_error('Short sequence - at least 2 bytes expected, only 1 seen');
  21. return FALSE;
  22. }
  23. $ord1 = ord($chr{1});
  24. if ( $ord0 >= 192 && $ord0 <= 223 ) {
  25. return ( $ord0 - 192 ) * 64
  26. + ( $ord1 - 128 );
  27. }
  28. if ( !isset($chr{2}) ) {
  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
  35. + ($ord1-128)*64
  36. + ($ord2-128);
  37. }
  38. if ( !isset($chr{3}) ) {
  39. trigger_error('Short sequence - at least 4 bytes expected, only 3 seen');
  40. return FALSE;
  41. }
  42. $ord3 = ord($chr{3});
  43. if ($ord0>=240 && $ord0<=247) {
  44. return ($ord0-240)*262144
  45. + ($ord1-128)*4096
  46. + ($ord2-128)*64
  47. + ($ord3-128);
  48. }
  49. if ( !isset($chr{4}) ) {
  50. trigger_error('Short sequence - at least 5 bytes expected, only 4 seen');
  51. return FALSE;
  52. }
  53. $ord4 = ord($chr{4});
  54. if ($ord0>=248 && $ord0<=251) {
  55. return ($ord0-248)*16777216
  56. + ($ord1-128)*262144
  57. + ($ord2-128)*4096
  58. + ($ord3-128)*64
  59. + ($ord4-128);
  60. }
  61. if ( !isset($chr{5}) ) {
  62. trigger_error('Short sequence - at least 6 bytes expected, only 5 seen');
  63. return FALSE;
  64. }
  65. if ($ord0>=252 && $ord0<=253) {
  66. return ($ord0-252) * 1073741824
  67. + ($ord1-128)*16777216
  68. + ($ord2-128)*262144
  69. + ($ord3-128)*4096
  70. + ($ord4-128)*64
  71. + (ord($chr{5})-128);
  72. }
  73. if ( $ord0 >= 254 && $ord0 <= 255 ) {
  74. trigger_error('Invalid UTF-8 with surrogate ordinal '.$ord0);
  75. return FALSE;
  76. }
  77. }