Convert.class.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. namespace KIF\Data;
  3. use KIF\Verify;
  4. use Exception;
  5. /**
  6. * 转换数据类,如将 字符表示的ip转换为整数表示的
  7. * @author gaoxiaogang@gmail.com
  8. *
  9. */
  10. class Convert {
  11. /**
  12. * 字型型ip转换为长整型
  13. * example:Convert::ip2long("59.151.9.90");
  14. * @param string $ip
  15. * @return int 32位无符号整型
  16. */
  17. static public function ip2long($ip) {
  18. $array = @explode('.', $ip);
  19. if (count($array) != 4) {
  20. return false;
  21. }
  22. $long = 0;
  23. $strBin = '';
  24. foreach ($array as $k => $v) {
  25. if ($v > 255) {
  26. return false;
  27. }
  28. $long += $v * pow(2, 8 * (3 - $k));
  29. }
  30. return $long;
  31. }
  32. /**
  33. * 将整数值转换成ip
  34. * example:Convert::long2ip(999754074)
  35. * @param int $long
  36. * @return string
  37. */
  38. static public function long2ip($long) {
  39. if (!Verify::int($long) || $long < 0 || $long >= pow(2, 32)) {
  40. return false;
  41. }
  42. $strBin = base_convert($long, 10, 2);
  43. $strBin = str_repeat('0', 32-strlen($strBin)) . $strBin;
  44. $arrTmpV = str_split($strBin, 8);
  45. foreach ($arrTmpV as & $tmpV) {
  46. $tmpV = base_convert($tmpV, 2, 10);
  47. }
  48. $ip = join('.', $arrTmpV);
  49. return $ip;
  50. }
  51. /**
  52. * utf8编码转gbk
  53. * @param string $u8str
  54. * @throws Exception 'SystemError:convert encoding fail!'
  55. * @return string
  56. */
  57. static public function u82gb($u8str) {
  58. if (function_exists('mb_convert_encoding')) {
  59. return mb_convert_encoding($u8str, 'gbk', 'utf-8');
  60. } elseif (function_exists('iconv')) {
  61. return iconv('utf-8', 'gbk', $u8str);
  62. } else {
  63. throw new Exception('SystemError:convert encoding fail!');
  64. }
  65. }
  66. /**
  67. * gbk编码转utf8
  68. * @param string $gbstr
  69. * @throws Exception 'SystemError:convert encoding fail!'
  70. * @return string
  71. */
  72. static public function gb2u8($gbstr) {
  73. if (function_exists('mb_convert_encoding')) {
  74. return mb_convert_encoding($gbstr, 'utf-8', 'gbk');
  75. } elseif (function_exists('iconv')) {
  76. return iconv('gbk', 'utf-8', $gbstr);
  77. } else {
  78. throw new Exception('SystemError:convert encoding fail!');
  79. }
  80. }
  81. /**
  82. * gbk编码数组转utf8
  83. * @param array $gbarr
  84. * @return array
  85. */
  86. static public function gb2u8ofarr(array $gbarr) {
  87. if (empty($gbarr)) {
  88. return $gbarr;
  89. }
  90. if (!is_array($gbarr)) {
  91. $result = self::gb2u8($gbarr);
  92. } else {
  93. foreach ($gbarr as $tmpkey => $tmpval) {
  94. if (is_array($tmpval)) {
  95. $tmp_arr = self::gb2u8ofarr($tmpval);
  96. $result[$tmpkey] = $tmp_arr;
  97. } else {
  98. $result[$tmpkey] = self::gb2u8($tmpval);
  99. }
  100. }
  101. }
  102. return $result;
  103. }
  104. /**
  105. * utf8编码数组转gbk
  106. * @param array $u8arr
  107. * @return array
  108. */
  109. static public function u82gbofarr(array $u8arr) {
  110. if (empty($u8arr)) {
  111. return $u8arr;
  112. }
  113. if (!is_array($u8arr)) {
  114. $result = self::u82gb($u8arr);
  115. } else {
  116. foreach ($u8arr as $tmpkey => $tmpval) {
  117. if (is_array($tmpval)) {
  118. $tmp_arr = self::u82gbofarr($tmpval);
  119. $result[$tmpkey] = $tmp_arr;
  120. } else {
  121. $result[$tmpkey] = self::u82gb($tmpval);
  122. }
  123. }
  124. }
  125. return $result;
  126. }
  127. /**
  128. * 转换为以元为单位的金额表示。即2位小数的浮点数
  129. * example:Convert::toMoney("19.221");
  130. * @param mixed $str
  131. * @param Boolean $isStripSuffixZero 是否去掉后缀的0
  132. * @return float 2位小数的浮点数
  133. */
  134. static public function toMoney($str, $isStripSuffixZero = true) {
  135. $money = sprintf('%.2f', $str);
  136. if ($isStripSuffixZero) {
  137. $money = str_replace('.00', '', $money);
  138. }
  139. return $money;
  140. }
  141. /**
  142. *
  143. * 解密字符串成id
  144. * @param string $str
  145. * @return false | int
  146. */
  147. static public function decryptStr2Id($str) {
  148. if (!preg_match('#^([a-j]+)[\x6b-\x7a]([a-j]+)$#', $str, $match)) {
  149. return false;
  150. }
  151. $id_1 = self::char2num($match[1]);
  152. $id_2 = self::char2num($match[2]);
  153. $id = $id_2 - $id_1;
  154. if (!Verify::unsignedInt($id)) {
  155. return false;
  156. }
  157. return $id;
  158. }
  159. /**
  160. *
  161. * 加密id成字符串,用于保护id不被人发现规律
  162. * @param int $id
  163. * @return false | string
  164. */
  165. static public function encryptId2Str($id) {
  166. if (!Verify::unsignedInt($id)) {
  167. return false;
  168. }
  169. $id_1 = rand(pow(10, strlen($id)-1), pow(10, strlen($id)));
  170. $id_2 = $id_1 + $id;
  171. $dash = chr(rand(0x6b, 0x7a));
  172. return self::num2char($id_1) . $dash . self::num2char($id_2);
  173. }
  174. /**
  175. *
  176. * 数字转换成对应的字符
  177. * @param int $num
  178. * @return string
  179. */
  180. static private function num2char($num) {
  181. $str = '';
  182. $num = (string) $num;
  183. for ($i = 0; $i < strlen($num); $i++) {
  184. $str .= chr($num[$i] + 97);
  185. }
  186. return $str;
  187. }
  188. /**
  189. *
  190. * 将字符转换成对应的数字
  191. * @param string $str
  192. * @return int
  193. */
  194. static private function char2num($str) {
  195. $num = '';
  196. for ($i = 0; $i < strlen($str); $i++) {
  197. $num .= (ord($str[$i]) - 97);
  198. }
  199. return $num;
  200. }
  201. }