spliter.class.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * The spliter class file of chanzhiEPS.
  4. *
  5. * @copyright Copyright 2013-2014 青岛息壤网络信息有限公司 (QingDao XiRang Network Infomation Co,LTD www.xirangit.com)
  6. * @license LGPL
  7. * @author Xiying Guan <guanxiying@xirangit.com>
  8. * @package spliter
  9. * @version $Id$
  10. * @link http://www.chanzhi.org
  11. * @see http://www.cnblogs.com/chenwenbiao/archive/2011/08/11/2134503.html
  12. * @see http://stackoverflow.com/questions/9361303/can-i-get-the-unicode-value-of-a-character-or-vise-versa-with-php
  13. */
  14. class spliter
  15. {
  16. /**
  17. * Split a utf-8 string into words, computing unicode for every word.
  18. *
  19. * @param string $string
  20. * @access public
  21. * @return array
  22. */
  23. public function utf8Split($string)
  24. {
  25. $string = strtolower($string);
  26. $i = 0;
  27. $length = strlen($string);
  28. $dict = array();
  29. $words = '';
  30. $offset = 0;
  31. while($i <= $length)
  32. {
  33. $letter = substr($string, $i, 1);
  34. $ord = ord($letter);
  35. /* The first letter is ascii, try to get a word. */
  36. if($ord >= 0 && $ord <= 191)
  37. {
  38. $i ++;
  39. /* Stitching content in the case of words or Spaces. */
  40. if($this->isLetter($letter))
  41. {
  42. $word = $letter;
  43. while($i <= $length)
  44. {
  45. $letter = substr($string, $i, 1);
  46. if(!$this->isLetter($letter)) break;
  47. $word .= $letter;
  48. $i++;
  49. }
  50. /* Process intigers. */
  51. if(is_numeric($word) and (strpos($word, '.') === false)) $word = "|" . $word . "|";
  52. $word = str_pad(strtolower($word), 5, '_');
  53. $words .= ' ' . $word;
  54. }
  55. elseif($ord == 32)
  56. {
  57. $words .= ' ';
  58. }
  59. continue;
  60. }
  61. if($ord >= 192 && $ord <= 223) $offset = 2;
  62. if($ord >= 224 && $ord <= 239) $offset = 3;
  63. if($ord >= 240 && $ord <= 247) $offset = 4;
  64. if($ord >= 248 && $ord <= 251) $offset = 5;
  65. if($ord >= 252 && $ord <= 253) $offset = 6;
  66. if($offset >= 2)
  67. {
  68. $letter = substr($string, $i, $offset);
  69. $unicode = $this->unicode($letter);
  70. if(strlen($unicode) == 5)
  71. {
  72. $dict[$unicode] = $letter;
  73. $words .= ' ' . $unicode;
  74. }
  75. else
  76. {
  77. /* When the current word has a corresponding number in the dictionary table, concatenate a space before it. */
  78. if(is_numeric(substr($words, strlen($words) - 1, 1)))
  79. {
  80. $words .= ' ' . $letter;
  81. }
  82. else
  83. {
  84. $words .= $letter;
  85. }
  86. }
  87. $i += $offset;
  88. }
  89. }
  90. return array('dict' => $dict, 'words' => $words);
  91. }
  92. /**
  93. * Return unicode value for a char.
  94. *
  95. * @param string $c
  96. * @access public
  97. * @return int
  98. */
  99. public function unicode($c)
  100. {
  101. if(ord($c[0]) >= 0 && ord($c[0]) <= 127) return ord($c[0]);
  102. if(ord($c[0]) >= 192 && ord($c[0]) <= 223) return (ord($c[0]) - 192) * 64 + (ord($c[1]) - 128);
  103. if(ord($c[0]) >= 224 && ord($c[0]) <= 239) return (ord($c[0]) - 224) * 4096 + (ord($c[1]) - 128) * 64 + (ord($c[2]) - 128);
  104. if(ord($c[0]) >= 240 && ord($c[0]) <= 247) return (ord($c[0]) - 240) * 262144 + (ord($c[1]) - 128) * 4096 + (ord($c[2]) - 128) * 64 + (ord($c[3]) - 128);
  105. if(ord($c[0]) >= 248 && ord($c[0]) <= 251) return (ord($c[0]) - 248) * 16777216 + (ord($c[1]) - 128) * 262144 + (ord($c[2]) - 128) * 4096 + (ord($c[3]) - 128) * 64 + (ord($c[4]) - 128);
  106. if(ord($c[0]) >= 252 && ord($c[0]) <= 253) return (ord($c[0]) - 252) * 1073741824 + (ord($c[1]) - 128) * 16777216 + (ord($c[2]) - 128) * 262144 + (ord($c[3]) - 128) * 4096 + (ord($c[4]) - 128) * 64 + (ord($c[5]) - 128);
  107. if(ord($c[0]) >= 254 && ord($c[0]) <= 255) return false;
  108. return false;
  109. }
  110. /**
  111. * Judge a char is Letter or not.
  112. *
  113. * @param string $letter
  114. * @access public
  115. * @return bool
  116. */
  117. public function isLetter($letter)
  118. {
  119. $ord = ord($letter);
  120. if($ord >= ord('a') and $ord <= ord('z')) return true;
  121. if($ord >= ord('A') and $ord <= ord('Z')) return true;
  122. if($ord >= ord(0) and $ord <= ord(9)) return true;
  123. if($letter and strpos('._/->:<?&', $letter) !== false) return true;
  124. return false;
  125. }
  126. }