Skip to content

components str

tolizhan edited this page Aug 3, 2022 · 4 revisions

文本封装

of_base_com_str::mcrypt($key, $txt, $type, $mode = array()) mcrypt 加密与解密

  • $keystring
    加密解密文本密码
  • $txtstring
    需要加解密的文本
  • $typeint, boolean
    加密解密标识,1=加密, 0=解密, true=base64加密, false=base64解密
  • $modearray
    加密模式(http://php.net/manual/zh/mcrypt.ciphers.php) {
        "type" : 算法(algorithm),
               MCRYPT_RIJNDAEL_128 :  (默认)AES
               MCRYPT_3DES         :  DES
               ...
        "mode" : 模式, 默认 MCRYPT_MODE_ECB, 还可大写(MCRYPT_MODE_modename : "ecb","cbc","cfb","ofb","nofb" 和 "stream")
        "rand" :&随机数, 默认 mcrypt_create_iv(算法位, MCRYPT_RAND)
    }
<?php
//AES_ECB 算法
mcrypt('密码', '测试', true);
随机加密字符,如:cqwPeOAp0LtFEE9kH205IGAbOraQiaEXfvJZNnzcQyI=
rc4('密码', mcrypt('密码', '测试', true), false);
测试

//AES_CBC 算法 $temp = of_base_com_str::mcrypt('密码', '测试', true, array(     'mode' => MCRYPT_MODE_CBC,     'rand' => &$vector                                          //会生成CBC的项量 ));

$temp = of_base_com_str::mcrypt('密码', $temp, false, array(     'mode' => MCRYPT_MODE_CBC,     'rand' => $vector                                           //将CBC项量赋值解密 ));

of_base_com_str::rc4($pwd, $txt, $base64 = null, $level = 256) RC4 加密与解密

  • $pwdstring
    加密解密文本密码
  • $txtstring
    需要加解密的文本
  • $base64null, boolean
    加密解密标识,null=无编码加密解密, true=base64加密, false=base64解密
  • $levelint
    加密级别 1=简单线性加密, >1 = RC4加密数字越大越安全越慢, 默认=256
<?php
rc4('密码', '测试', true);
随机加密字符,如:Yegw4WXcMOth/DvC
rc4('密码', rc4('密码', '测试', true), false);
测试

of_base_com_str::realpath($path) 获取真实路径,去掉多余的'/./', '/../', '//'

  • pathstring
    指定源路径
<?php
of_base_com_str::realpath('/xx/.././cs.php') === '/xx/cs.php';    //返回规范化的绝对路径名

of_base_com_str::strArrPos(&$str, $matches, $offset = 0) 从字符串中提取指定数组中最先出现的位置

  • strstring
    被搜索的字符串
  • matchesarray
    匹配数组,优先级从高到底 {
        匹配字符串(不区分大小写) : 是否受'&#039;影响,true=是,false=否
    }
  • offsetint
    偏移量
<?php
/**
  没找到返回false,否则{'match' : 最先出现的字符串, 'position' : 出现的位置}
  $data 的结果为 {'match' => 'c', 'position' => 4}
 */
$str = 'a\s\cms';
$data = of_base_com_str::strArrPos($str, array(
    'c'  => false,                                       //c的优先级比cm高
    'cm' => false,                                       //与c的查询位置相同
    's'  => true                                         //最后一个s是有效的
));

of_base_com_str::strsub($str, $start, $length = null) 字符串按位截取,一个utf8为3位,同时保证截取后的字符串完整

  • strstring
    指定截取的UTF8编码字符串
  • startint, array
    数字=起始长度, 数组=额外参数{"start" : 起始长度, "length" : 截取长度, "overflow" : 额外溢出字符串}
  • lengthint
    截取长度. null(默认)=截取到最后
<?php
class demo_ofControllers extends L
{
    public function getEscape() {
        echo $this->_str->strsub('我a是m', 2) === '是m';
    }
}

of_base_com_str::uniqid($prefix = '', $isShow = true, $minLen = 3) 获取更具唯一性的ID

  • prefixstring
    编码前缀, 不同前缀并发互不影响, ''=全局32位小写唯一编码, 其它=系统级可排序唯一短编码
  • isShowint, bool, string
    功能操作
    数字   = 代替minLen参数,
    布尔   = 显示前缀, true=显示, false=隐藏
    字符串 = 时间结构, 用"&quot;转义, 默认"ymdHis", 如: "\y\m\dymd-"
  • minLenint
    自增值最小长度, prefix不为空时有效, 默认3
<?php
echo of_base_com_str::uniqid();                     //32位小写编码: 14e83f89ea6a4ac6de78576cc79c1734
echo of_base_com_str::uniqid('abc');                //18位大写编码: 大写prefix + 两位年月日时分秒12位 + 3位计数: ABC180130232907001
echo of_base_com_str::uniqid('abc', 5);             //20位大写编码: 大写prefix + 两位年月日时分秒12位 + 5位计数: ABC18013023174600002
echo of_base_com_str::uniqid('abc', true, 3);       //15位大写编码: 大写prefix + 两位年月日时分秒12位 + 3位计数: ABC180130231746002
echo of_base_com_str::uniqid('abc', false, 5);      //17位大写编码: 两位年月日时分秒12位 + 5位计数: 18013023174600004
echo of_base_com_str::uniqid('abc', '\y\m\dymd')    //12位格式编码: ymd开头 + 两位年月日 + 3位计数: ymd190618001
Clone this wiki locally