PageRenderTime 26ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/Framework/Common/String.php

http://lxsphp.googlecode.com/
PHP | 238 lines | 123 code | 22 blank | 93 comment | 25 complexity | 085e6d5b32efacc644c941317b05be47 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * ??????
  4. *
  5. * @version $Id: String.php 276 2012-01-17 09:42:32Z linsir123 $
  6. * @package Common
  7. */
  8. class lpString
  9. {
  10. /**
  11. * [??]????
  12. *
  13. * @param string $string ???????
  14. * @return bool
  15. * @static
  16. */
  17. static function isEmail($string)
  18. {
  19. $pattern = "/^([a-z0-9]+[\.\-_]?)*[a-z0-9]+@([a-z0-9]+[\.\-_]?)*[a-z0-9]+\.[a-z]{2,4}$/i";
  20. return preg_match($pattern, $string);
  21. }
  22. /**
  23. * [??]utf-8??
  24. *
  25. * @param string $string ???????
  26. * @return bool
  27. * @static
  28. */
  29. static function isUtf8($string)
  30. {
  31. return (utf8_encode(utf8_decode($string)) == $string);
  32. }
  33. /**
  34. * [??]????
  35. *
  36. * @param string $string ???????
  37. * @return bool
  38. * @static
  39. */
  40. static function isDatetime($string)
  41. {
  42. $pattern = "/^[0-9]{4}(\-|\/)[0-9]{1,2}(\\1)[0-9]{1,2}(|\s+[0-9]{1,2}(:[0-9]{1,2}){0,2})$/i";
  43. return preg_match($pattern, $string);
  44. }
  45. /**
  46. * [??]?????
  47. *
  48. * @param string $string ???????
  49. * @param string $sep ??????
  50. * @return bool
  51. * @static
  52. */
  53. static function isDate($string, $sep = '-')
  54. {
  55. $parts = explode($sep, $string);
  56. if (count($parts) == 3) {
  57. if (checkdate(intval($parts[1]), intval($parts[2]), intval($parts[0])))
  58. return true;
  59. }
  60. return false;
  61. }
  62. /**
  63. * ????????
  64. *
  65. * @param string $string ???????
  66. * @return string
  67. * @static
  68. */
  69. static function trim($string)
  70. {
  71. return preg_replace("/\s/", '', trim($string));
  72. }
  73. /**
  74. * ??????????
  75. *
  76. * @param mixed $var ??????
  77. * @param bool $force ??????
  78. * @return mixed
  79. * @static
  80. */
  81. static function addslashes($var, $force = false)
  82. {
  83. if ( ! get_magic_quotes_gpc() || $force)
  84. $var = is_array($var) ?
  85. array_map(array('lpString', 'addslashes'), $var) :
  86. addslashes($var);
  87. return $var;
  88. }
  89. /**
  90. * ??????????????
  91. *
  92. * @param mixed $var ??????
  93. * @return mixed
  94. * @static
  95. */
  96. static function stripslashes($var)
  97. {
  98. $var = is_array($var) ?
  99. array_map(array('lpString', 'stripslashes'), $var) :
  100. stripslashes($var);
  101. return $var;
  102. }
  103. /**
  104. * SQL??
  105. *
  106. * @param mixed $var ??????
  107. * @return mixed
  108. * @static
  109. */
  110. static function dbQuote($var)
  111. {
  112. $var = is_array($var) ?
  113. array_map(array('lpString', 'dbQuote'), $var) :
  114. '`'.trim($var).'`';
  115. return $var;
  116. }
  117. /**
  118. * ??SQL??
  119. *
  120. * @param mixed $var ??????
  121. * @return mixed
  122. * @static
  123. */
  124. static function sqlEscape($var)
  125. {
  126. if (is_bool($var))
  127. $var = $var ? 1 : 0;
  128. elseif (is_null($var))
  129. $var = 'NULL';
  130. elseif (is_string($var))
  131. $var = "'".mysql_escape_string($var)."'";
  132. elseif (is_array($var))
  133. $var = array_map(array('lpString', 'sqlEscape'), $var);
  134. return $var;
  135. }
  136. /**
  137. * ?????(???????????????UTF-8?????)
  138. *
  139. * @param string $string ???????
  140. * @param int $length ????,?????
  141. * @param string $omission ??????????
  142. * @param string $code ??
  143. * @return string
  144. * @static
  145. */
  146. static function substr($string, $length = 50, $omission = ' ...', $code = 'UTF-8')
  147. {
  148. $string = preg_replace("/\s/", ' ', strip_tags($string));
  149. $string = str_replace(array("&nbsp;"), array(""), $string);
  150. $string = htmlspecialchars_decode($string);
  151. if (empty($length) || strlen($string) <= $length)
  152. return $string;
  153. ///
  154. $byteLen = 0;
  155. $wordString = '';
  156. if (strtoupper($code) == 'UTF-8') {
  157. preg_match_all("/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/", $string, $words);
  158. $count = count($words[0]);
  159. for ($i = 0; $i < $count; $i++) {
  160. $wordString .= $words[0][$i];
  161. if (ord($words[0][$i]) >= 128)
  162. $byteLen += 2;
  163. else
  164. $byteLen++;
  165. if ($byteLen >= $length)
  166. return $wordString . $omission;
  167. }
  168. } else {
  169. $count = strlen($string);
  170. for ($i = 0; $i < $count; $i++) {
  171. if(ord(substr($string, $i, 1)) >= 128) {
  172. $byteLen += 2;
  173. $wordString .= substr($string, $i, 2);
  174. $i++;
  175. } else {
  176. $byteLen++;
  177. $wordString .= substr($string, $i, 1);
  178. }
  179. if ($byteLen >= $length)
  180. return $wordString . $omission;
  181. }
  182. }
  183. return $string;
  184. }
  185. /**
  186. * ?16???????RGB??
  187. * // `Captcha.php`,`Image.php`?????
  188. *
  189. * @param string $color ???(16??)
  190. * @param string $defualt ?????(16??)
  191. * @return array
  192. * @static
  193. */
  194. static function hex2rgb($color, $defualt = 'ffffff')
  195. {
  196. $color = strtolower($color);
  197. if (substr($color, 0, 2) == '0x')
  198. $color = substr($color, 2);
  199. elseif (substr($color, 0, 1) == '#')
  200. $color = substr($color, 1);
  201. ///
  202. $l = strlen($color);
  203. if ($l == 3) {
  204. $r = hexdec(substr($color, 0, 1));
  205. $g = hexdec(substr($color, 1, 1));
  206. $b = hexdec(substr($color, 2, 1));
  207. return array($r, $g, $b);
  208. } elseif ($l != 6)
  209. $color = $defualt;
  210. ///
  211. $r = hexdec(substr($color, 0, 2));
  212. $g = hexdec(substr($color, 2, 2));
  213. $b = hexdec(substr($color, 4, 2));
  214. return array($r, $g, $b);
  215. }
  216. }