標籤:
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; }
php常用方法一