[ Index ]

PHP Cross Reference of MantisBT

title

Body

[close]

/library/utf8/ -> ord.php (source)

   1  <?php
   2  /**
   3  * @version $Id: ord.php,v 1.4 2006/09/11 15:22:54 harryf Exp $
   4  * @package utf8
   5  * @subpackage strings
   6  */
   7  
   8  //---------------------------------------------------------------
   9  /**
  10  * UTF-8 aware alternative to ord
  11  * Returns the unicode ordinal for a character
  12  * @param string UTF-8 encoded character
  13  * @return int unicode ordinal for the character
  14  * @see http://www.php.net/ord
  15  * @see http://www.php.net/manual/en/function.ord.php#46267
  16  */
  17  function utf8_ord($chr) {
  18      
  19      $ord0 = ord($chr);
  20      
  21      if ( $ord0 >= 0 && $ord0 <= 127 ) {
  22          return $ord0;
  23      }
  24      
  25      if ( !isset($chr{1}) ) {
  26          trigger_error('Short sequence - at least 2 bytes expected, only 1 seen');
  27          return FALSE;
  28      }
  29      
  30      $ord1 = ord($chr{1});
  31      if ( $ord0 >= 192 && $ord0 <= 223 ) {
  32          return ( $ord0 - 192 ) * 64 
  33              + ( $ord1 - 128 );
  34      }
  35      
  36      if ( !isset($chr{2}) ) {
  37          trigger_error('Short sequence - at least 3 bytes expected, only 2 seen');
  38          return FALSE;
  39      }
  40      $ord2 = ord($chr{2});
  41      if ( $ord0 >= 224 && $ord0 <= 239 ) {
  42          return ($ord0-224)*4096 
  43              + ($ord1-128)*64 
  44                  + ($ord2-128);
  45      }
  46      
  47      if ( !isset($chr{3}) ) {
  48          trigger_error('Short sequence - at least 4 bytes expected, only 3 seen');
  49          return FALSE;
  50      }
  51      $ord3 = ord($chr{3});
  52      if ($ord0>=240 && $ord0<=247) {
  53          return ($ord0-240)*262144 
  54              + ($ord1-128)*4096 
  55                  + ($ord2-128)*64 
  56                      + ($ord3-128);
  57      
  58      }
  59      
  60      if ( !isset($chr{4}) ) {
  61          trigger_error('Short sequence - at least 5 bytes expected, only 4 seen');
  62          return FALSE;
  63      }
  64      $ord4 = ord($chr{4});
  65      if ($ord0>=248 && $ord0<=251) {
  66          return ($ord0-248)*16777216 
  67              + ($ord1-128)*262144 
  68                  + ($ord2-128)*4096 
  69                      + ($ord3-128)*64 
  70                          + ($ord4-128);
  71      }
  72      
  73      if ( !isset($chr{5}) ) {
  74          trigger_error('Short sequence - at least 6 bytes expected, only 5 seen');
  75          return FALSE;
  76      }
  77      if ($ord0>=252 && $ord0<=253) {
  78          return ($ord0-252) * 1073741824 
  79              + ($ord1-128)*16777216 
  80                  + ($ord2-128)*262144 
  81                      + ($ord3-128)*4096 
  82                          + ($ord4-128)*64 
  83                              + (ord($c{5})-128);
  84      }
  85      
  86      if ( $ord0 >= 254 && $ord0 <= 255 ) { 
  87          trigger_error('Invalid UTF-8 with surrogate ordinal '.$ord0);
  88          return FALSE;
  89      }
  90      
  91  }
  92  


Generated: Thu Jul 28 15:48:31 2011 Cross-referenced by PHPXref 0.7