看啥推荐读物
专栏名称: JunChow520
阅读提升上限,实践突破下限。
目录
今天看啥  ›  专栏  ›  JunChow520

Swoft 助手函数

JunChow520  · 简书  ·  · 2019-05-12 17:00

Swoft中可以为项目添加自定义的函数,助手函数位于/app/Helper/Functions.php文件内。添加的助手函数将在整个应用中生效。

$ vim /app/Helper/Functions.php

将字节转换为可读文本

if(!function_exists("format_bytes"))
{
    /**
     * 将字节转换为可读文本
     * @param int $bytes 字节大小
     * @param int $precision 小数保留位数
     * @param string $delimiter 分隔符
     * @return string
     */
    function format_bytes($bytes, $precision=2, $delimiter = "")
    {
        $units = ["B", "KB", "MB", "GB", "TB", "PB"];
        for($i=0; $i<count($units) && $bytes>=1024; $i++)
        {
            $bytes /= 1024;
        }
        return round($bytes, $precision).$delimiter.$units[$i];
    }
}

判断文件或文件夹是否可写

if(!function_exists("is_writeable"))
{
    /**
     * 判断文件或文件夹是否可写
     * @param    string $file 文件或目录
     * @return    bool
     */
    function is_writeable($file)
    {
        if(DIRECTORY_SEPARATOR === "/")
        {
            return is_writable($file);
        }
        if(!is_file($file) or (@fopen($file, "ab")===false))
        {
            return false;
        }
        if(is_dir($file))
        {
            $file = rtrim("/")."/".uniqid();
            $fp = @fopen($file, "ab");
            if($fp === false)
            {
                return false;
            }
            fclose($fp);

            @chmod($file, 0777);
            @unlink($file);
            return true;
        }
    }
}

中文字符串截取

if(!function_exists("cncut"))
{
    /**
     * 中文字符串截取
     * @param    string $str 字符串
     * @param    int $length 截取长度
     * @param    string $ellipsis 省略符号
     * @return    string
     */
    function cncut($str, $length, $ellipsis="...")
    {
        if(mb_strlen($str, "utf8")>$length)
        {
            return mb_substr($str, 0, $length, "utf8").$ellipsis;
        }
        return $str;
    }
}

秒数化为时分秒

if(!function_exists("format_seconds"))
{
    /**
     * 秒数转化为时分秒
     * @param    int $seconds 秒数
     * @return    string
     */
    function format_seconds($seconds)
    {
        if($seconds > 3600)
        {
            $hours = intval($seconds/3600);
            $minutes = $seconds % 3600;
            return $hours.":".gmstrftime("%M:%S",$minutes);
        }else{
            return gmstrftime("%H:%M:%S", $seconds);
        }
    }
}

生成全球唯一标识

if(!function_exists("uuid"))
{
    /**
     * 生成全球唯一标识
     * @return string
     */
    function uuid()
    {
        return sprintf(
    "%04x%04x-%04x-%04x-%04x-%04x%04x%04x",
            mt_rand(0, 0xffff),
            mt_rand(0, 0xffff),
            mt_rand(0, 0xffff),
            mt_rand(0, 0xffff),
            mt_rand(0, 0xffff),
            mt_rand(0, 0xffff),
            mt_rand(0, 0xffff),
            mt_rand(0, 0xffff)
        );
    }
}

随机数生成

if(!function_exists("rand"))
{
    /**
     * 随机数生成
     * @param string $type 类型 alpha/alnum/numeric/nozero/unique/md5/encrypt/sha1
     * @param int $length 长度
     * @return string
     */
    function rand($type = "numeric", $length = 6)
    {
        switch ($type)
        {
            case "alpha":
            case "alnum":
            case "numeric":
            case "nozero":
                switch($type)
                {
                    case "alpha": $pool = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";break;
                    case "alnum": $pool = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";break;
                    case "numeric": $pool = "0123456789";break;
                    case "nozero": $pool = "123456789";break;
                }
                return substr(str_shuffle(str_repeat($pool, ceil($length / strlen($pool)))), 0, $length);
            case "unique":
            case "md5":
                return md5(uniqid(mt_rand()));
            case "encrypt":
            case "sha1":
                return sha1(uniqid(mt_rand(), true));
        }
    }
}

未完待续...




原文地址:访问原文地址
快照地址: 访问文章快照