/Framework/Common/String.php
PHP | 238 lines | 123 code | 22 blank | 93 comment | 25 complexity | 085e6d5b32efacc644c941317b05be47 MD5 | raw file
Possible License(s): LGPL-2.1
- <?php
- /**
- * ??????
- *
- * @version $Id: String.php 276 2012-01-17 09:42:32Z linsir123 $
- * @package Common
- */
- class lpString
- {
- /**
- * [??]????
- *
- * @param string $string ???????
- * @return bool
- * @static
- */
- static function isEmail($string)
- {
- $pattern = "/^([a-z0-9]+[\.\-_]?)*[a-z0-9]+@([a-z0-9]+[\.\-_]?)*[a-z0-9]+\.[a-z]{2,4}$/i";
- return preg_match($pattern, $string);
- }
-
- /**
- * [??]utf-8??
- *
- * @param string $string ???????
- * @return bool
- * @static
- */
- static function isUtf8($string)
- {
- return (utf8_encode(utf8_decode($string)) == $string);
- }
-
- /**
- * [??]????
- *
- * @param string $string ???????
- * @return bool
- * @static
- */
- static function isDatetime($string)
- {
- $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";
- return preg_match($pattern, $string);
- }
-
- /**
- * [??]?????
- *
- * @param string $string ???????
- * @param string $sep ??????
- * @return bool
- * @static
- */
- static function isDate($string, $sep = '-')
- {
- $parts = explode($sep, $string);
- if (count($parts) == 3) {
- if (checkdate(intval($parts[1]), intval($parts[2]), intval($parts[0])))
- return true;
- }
-
- return false;
- }
-
- /**
- * ????????
- *
- * @param string $string ???????
- * @return string
- * @static
- */
- static function trim($string)
- {
- return preg_replace("/\s/", '', trim($string));
- }
-
- /**
- * ??????????
- *
- * @param mixed $var ??????
- * @param bool $force ??????
- * @return mixed
- * @static
- */
- static function addslashes($var, $force = false)
- {
- if ( ! get_magic_quotes_gpc() || $force)
- $var = is_array($var) ?
- array_map(array('lpString', 'addslashes'), $var) :
- addslashes($var);
-
- return $var;
- }
-
- /**
- * ??????????????
- *
- * @param mixed $var ??????
- * @return mixed
- * @static
- */
- static function stripslashes($var)
- {
- $var = is_array($var) ?
- array_map(array('lpString', 'stripslashes'), $var) :
- stripslashes($var);
-
- return $var;
- }
-
- /**
- * SQL??
- *
- * @param mixed $var ??????
- * @return mixed
- * @static
- */
- static function dbQuote($var)
- {
- $var = is_array($var) ?
- array_map(array('lpString', 'dbQuote'), $var) :
- '`'.trim($var).'`';
-
- return $var;
- }
-
- /**
- * ??SQL??
- *
- * @param mixed $var ??????
- * @return mixed
- * @static
- */
- static function sqlEscape($var)
- {
- if (is_bool($var))
- $var = $var ? 1 : 0;
- elseif (is_null($var))
- $var = 'NULL';
- elseif (is_string($var))
- $var = "'".mysql_escape_string($var)."'";
- elseif (is_array($var))
- $var = array_map(array('lpString', 'sqlEscape'), $var);
-
- return $var;
- }
-
- /**
- * ?????(???????????????UTF-8?????)
- *
- * @param string $string ???????
- * @param int $length ????,?????
- * @param string $omission ??????????
- * @param string $code ??
- * @return string
- * @static
- */
- static function substr($string, $length = 50, $omission = ' ...', $code = 'UTF-8')
- {
- $string = preg_replace("/\s/", ' ', strip_tags($string));
- $string = str_replace(array(" "), array(""), $string);
- $string = htmlspecialchars_decode($string);
- if (empty($length) || strlen($string) <= $length)
- return $string;
-
- ///
- $byteLen = 0;
- $wordString = '';
- if (strtoupper($code) == 'UTF-8') {
- 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);
- $count = count($words[0]);
- for ($i = 0; $i < $count; $i++) {
- $wordString .= $words[0][$i];
- if (ord($words[0][$i]) >= 128)
- $byteLen += 2;
- else
- $byteLen++;
-
- if ($byteLen >= $length)
- return $wordString . $omission;
- }
- } else {
- $count = strlen($string);
- for ($i = 0; $i < $count; $i++) {
- if(ord(substr($string, $i, 1)) >= 128) {
- $byteLen += 2;
- $wordString .= substr($string, $i, 2);
- $i++;
- } else {
- $byteLen++;
- $wordString .= substr($string, $i, 1);
- }
-
- if ($byteLen >= $length)
- return $wordString . $omission;
- }
- }
-
- return $string;
- }
-
- /**
- * ?16???????RGB??
- * // `Captcha.php`,`Image.php`?????
- *
- * @param string $color ???(16??)
- * @param string $defualt ?????(16??)
- * @return array
- * @static
- */
- static function hex2rgb($color, $defualt = 'ffffff')
- {
- $color = strtolower($color);
- if (substr($color, 0, 2) == '0x')
- $color = substr($color, 2);
- elseif (substr($color, 0, 1) == '#')
- $color = substr($color, 1);
-
- ///
- $l = strlen($color);
- if ($l == 3) {
- $r = hexdec(substr($color, 0, 1));
- $g = hexdec(substr($color, 1, 1));
- $b = hexdec(substr($color, 2, 1));
-
- return array($r, $g, $b);
- } elseif ($l != 6)
- $color = $defualt;
-
- ///
- $r = hexdec(substr($color, 0, 2));
- $g = hexdec(substr($color, 2, 2));
- $b = hexdec(substr($color, 4, 2));
- return array($r, $g, $b);
- }
- }