1.用户名用***替换 /** * 用户名中间用***替换 * @param string $str 需要替换的字符串 * @param int $len 需要替换的位数 * @param string $replace 需要替换成的内容,一般是*** */ public static function substr_cut($str,$len=1,$replace=‘***‘) { $strlen = mb_strlen($str, ‘utf-8‘); if($strlen < 2 || $strlen <= $len) { return $str.$replace; } else { $first = mb_substr($str, 0, $len, ‘utf-8‘); $last = mb_substr($str, -$len, $len, ‘utf-8‘); return $first.$replace.$last; } }
2.ajax输出 /** * 输出ajax数据 * @param string $msg 错误或成功提示信息 * @param boolean $status 状态 * @param object | array | string $data 需要返回的数据 * @param string $type 返回格式;默认json * @param string $callback js回调函数名,此参数不为空且类型为jsonp时返回jsonp * @return string | object string JSON encoded object */ public static function output($msg = null, $status = true, $data = null, $type = ‘json‘, $callback = ‘‘) { $response = array(); $response[‘status‘] = $status; if ($msg !== null) { $response[‘msg‘] = $msg; // 返回的提示信息 } if ($data !== null) { $response[‘data‘] = $data; // 返回的数据 } if (($type == ‘jsonp‘) && !empty($callback)) { echo $callback . ‘(‘ . json_encode( $response ) . ‘);‘; } else { // 输出 json 文本格式 echo json_encode($response); } Yii::app()->end(); }
3.截取字符串用... /** * 截取字符串,大于指定长度的字符串在截取之后,会输出三个小点 * @param string $string * @param int $length * @param string $encode * @return string */ public static function substr( $string, $length, $encode="utf-8") { if ( mb_strlen( $string, $encode ) <= $length ) return $string; $newString = mb_substr( $string, 0, $length, $encode ); $newString .= ‘...‘; return $newString; }
4.随机产生uuid /** * 生成UUID编码 */ public static function uuid(){ $chars = md5(uniqid(time().mt_rand(), true)); $uuid = substr($chars,0,8) . ‘-‘; $uuid .= substr($chars,8,4) . ‘-‘; $uuid .= substr($chars,12,4) . ‘-‘; $uuid .= substr($chars,16,4) . ‘-‘; $uuid .= substr($chars,20,12); return $uuid; }
5.防止重复提交 /** * 设置表单token,防止重复提交。 * @param $identify 唯一标记 * @return hash */ public static function hash($identify = ‘token‘) { $hash = uniqid(); Yii::app()->session->add($identify, $hash); return $hash; } /** * 验证token码是否正确 * @param $hash * @param $identify 唯一标记 * @return true|false */ public static function checkHash($hash, $identify = ‘token‘){ $result = false; $sessionHash = Yii::app()->session->get($identify); if (strnatcasecmp($sessionHash, $hash)===0) { $result = true; } Yii::app()->session->remove($identify); return $result; }
时间: 2024-10-06 03:30:25