php常用方法一

來源:互聯網
上載者:User

標籤:

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常用方法一

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.