php 常用函数 util Memcached

1.截取字符串
function get_utf8_word($string, $length = 80, $more = 1, $etc = '..') {
    $strcut = '';
    $strLength = 0; //  /usd
    $width = 0;
    if (strlen($string) > $length) {
        //将$length换算成实际UTF8格式编码下字符串的长度
        for ($i = 0; $i < $length; $i++) {
            if ($strLength >= strlen($string)) {
                break;
            }
            if ($width >= $length) {
                break;
            }
            //当检测到一个中文字符时
            if (ord($string[$strLength]) > 127) {
                $strLength += 3;
                $width += 2;              //大概按一个汉字宽度相当于两个英文字符的宽度
            } else {
                $strLength += 1;
                $width += 1;
            }
        }
        return substr($string, 0, $strLength) . $etc;
    } else {
        return $string;
    }
}

 


mx_set_cache('_home_zs_course', $zs_course);

$zs_course = mx_get_cache('_home_zs_course');


/*
* * @return Memcached */ function mx_get_memcached() { $connect = new Memcached; //声明一个新的memcached链接 $connect->setOption(Memcached::OPT_COMPRESSION, false); //关闭压缩功能 $connect->setOption(Memcached::OPT_BINARY_PROTOCOL, true); //使用binary二进制协议 $connect->addServer('ip...', 11211); //添加OCS实例地址及端口号 $connect->setSaslAuthData('f99d839f749911e4', '密码'); //设置OCS帐号密码进行鉴权 return $connect; } function mx_get_cache($key) { return null; $connect = mx_get_memcached(); $r = $connect->get($key); $connect->quit(); return $r; } function mx_set_cache($key, $value, $expire=36000) {return true; $connect = mx_get_memcached(); $r = $connect->set($key,$value,$expire); $connect->quit(); return $r; }

 

posted @ 2016-11-11 11:21  伊人世界  阅读(189)  评论(0编辑  收藏  举报
Fork me on GitHub