php开发常用的实用方法(第一节)(电商开发微信支付宝开发常用)

Posted 哈尔滨洛弘科技有限公司

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php开发常用的实用方法(第一节)(电商开发微信支付宝开发常用)相关的知识,希望对你有一定的参考价值。

php常用自定义实用方法(第一节)

电商开发、微信支付宝开发常用

1、int数字加密解密,根据时间戳将指定int生成随机字符

 /**
     * @param int $num
     * @return $this
     * 数字加密
     */
    function encode_num($num = 0)
    
        $num_len = strlen($num);
        if (!is_numeric($num) || $num_len > 15) 
            $this->data = false;
            $this->message = '请输入数字';
            return $this;
        
        list($start_rand, $end_rand, $nums, $pies) = [rand(100, 999), rand(10, 99), [], self::unique_rand(1, 9, 9)];
        for ($i = 0; $i < ($num_len / 2); $i++) 
            $nums[$pies[$i]] = ['num' => substr($num, $i * 2, 2), 'rand' => rand(1000, 7000), 'pie' => $pies[$i], 'i' => $i + 1];
        
        ksort($nums);
        $str = $start_rand;
        foreach ($nums as $maps) 
            list($rand, $d_rand) = [rand(10, 70), rand(1, 9)];
            $i = $rand + $maps['i'] + $d_rand;
            $len=strlen($maps['num']);
            $vs = ($maps['num'] * $maps['pie']) + $maps['rand'] + $start_rand - ($end_rand * $maps['i']) + $i;
            $str .= $maps['rand'] . $maps['pie'] . $rand . $i . $d_rand .$len .$vs;
        
        return $str . $end_rand;
    

    /**
     * @param int $num
     * @return $this
     * 数字解密
     */
    function decode_num($num = 0)
    
        if (!is_numeric($num)) 
            return false;
        
        if (empty($num)) $num = $this->data;
        list($start_rand, $end_rand, $num_len, $encode, $nums) = [ceil(substr($num, 0, 3)), ceil(substr($num, -2)), ceil(strlen($num) - 5), substr(substr($num, 3), 0, -2), []];
        for ($i = 0; $i < ceil($num_len / 15); $i++) 
            $nums[] = substr($encode, $i * 15, 15);
        
        $n = [];
        foreach ($nums as $maps) 
            list($maps_rand, $pie, $rand, $i, $d_rand, $vs,$len) = [ceil(substr($maps, 0, 4)), ceil(substr($maps, 4, 1)), substr($maps, 5, 2), intval(substr($maps, 7, 2)), substr($maps, 9, 1), ceil(substr($maps, 11)),ceil(substr($maps,10,1))];
            $rule_i = intval($i - $rand - $d_rand);
            $v = ceil(($vs - $i + ($end_rand * $rule_i) - $start_rand - $maps_rand) / $pie);
            if($len==2 && strlen($v)==1) $v='0'.$v;
            $n[$rule_i] = $v;
        
        ksort($n);
        return implode($n, '');
    

2、生成一定数量的不重复的随机数

  /**
     * array unique_rand( int $min, int $max, int $num )
     * 生成一定数量的不重复随机数
     * $min 和 $max: 指定随机数的范围
     * $num: 指定生成数量
     */
    function unique_rand($min, $max, $num)
    
        $count = 0;
        $return = array();
        while ($count < $num) 
            $return[] = mt_rand($min, $max);
            $return = array_flip(array_flip($return));
            $count = count($return);
        
        shuffle($return);
        return $return;
    

3、驼峰和下滑线转换方法

   /**
     * @param $data
     * @return mixed
     * 下滑线转驼峰
     */
    public static function convert2Underline($data)
    
        $data = preg_replace_callback('/([-_]+([a-z]1))/i', function ($matches) 
            return strtoupper($matches[2]);
        , $data);
        return $data;
    

    /**
     * 驼峰转下划线
     * @param $data
     * @return mixed
     */
    public function hump2ToLine($data)
    
        $data = preg_replace_callback('/([A-Z]1)/', function ($matches) 
            return '_' . strtolower($matches[0]);
        , $data);
        return $data;
    

4、获取汉字/英文首字母

    /**
     * @param $data
     * @return string
     * 获取汉字/英文首字母
     */
    function Str2First($data)
    
        $data = iconv("UTF-8", "gb2312", $data);//编码转换
        if (preg_match("/^[\\x7f-\\xff]/", $data)) 
            $fchar = ord($data0);
            if ($fchar >= ord("A") and $fchar <= ord("z")) return strtoupper($data0);
            $a = $data;
            $val = ord($a0) * 256 + ord($a1) - 65536;
            if ($val >= -20319 and $val <= -20284) return "A";
            if ($val >= -20283 and $val <= -19776) return "B";
            if ($val >= -19775 and $val <= -19219) return "C";
            if ($val >= -19218 and $val <= -18711) return "D";
            if ($val >= -18710 and $val <= -18527) return "E";
            if ($val >= -18526 and $val <= -18240) return "F";
            if ($val >= -18239 and $val <= -17923) return "G";
            if ($val >= -17922 and $val <= -17418) return "H";
            if ($val >= -17417 and $val <= -16475) return "J";
            if ($val >= -16474 and $val <= -16213) return "K";
            if ($val >= -16212 and $val <= -15641) return "L";
            if ($val >= -15640 and $val <= -15166) return "M";
            if ($val >= -15165 and $val <= -14923) return "N";
            if ($val >= -14922 and $val <= -14915) return "O";
            if ($val >= -14914 and $val <= -14631) return "P";
            if ($val >= -14630 and $val <= -14150) return "Q";
            if ($val >= -14149 and $val <= -14091) return "R";
            if ($val >= -14090 and $val <= -13319) return "S";
            if ($val >= -13318 and $val <= -12839) return "T";
            if ($val >= -12838 and $val <= -12557) return "W";
            if ($val >= -12556 and $val <= -11848) return "X";
            if ($val >= -11847 and $val <= -11056) return "Y";
            if ($val >= -11055 and $val <= -10247) return "Z";
         else 
            return '#';
        
    

5、生成随机订单号

   /**
     * @param int $digits /单号位数
     * @param string $prefix /单号长度
     * @return string
     * 创建随机单号
     */
    public function create_order_number($digits = 24, $prefix = '')
    
        $date = date('YmdHis');
        $digits = intval($digits);
        if ($digits < 14) return '随机单号大于14字符';
        $digits = $digits - strlen($date) - strlen($prefix);
        if ($digits < 0) return '随机单号生成失败';
        $rand = '';
        $num = floor($digits / 10);
        for ($i = 0; $i < $num; $i++) 
            $rand .= str_pad(mt_rand(1, (int)9999999999), 10, '0', STR_PAD_LEFT);
        
        if ($digits % 10 != 0) 
            $rand .= str_pad(mt_rand(1, (int)substr(9999999999, 0, $digits - ($num * 10))), $digits - ($num * 10), '0', STR_PAD_LEFT);
        
        $order_number = $prefix . $date . $rand;
        return $order_number;
    

6、字符串的截取

    /**
     * @param $string /要截取的字符串
     * @param $start /开始字符
     * @param $end /结束字符
     * @return string
     * 截取两个字符串之间的数据
     */
    public function get_string_between($string, $start, $end=null)
    
    	$string = ' ' . $string;
    	$ini = strpos($string, $start);
    	if ($ini == 0) return '';
    	$ini += strlen($start);
    	$len = is_null($end)?strlen($string)-$ini:strpos($string, $end, $ini) - $ini;
    	return substr($string, $ini, $len);
	

7、只替换第一个字符

  /**
     * @param $needle 搜索字符
     * @param $replace 替换的字符
     * @param $haystack 要替换字符
     * @return mixed
     * 只替换一次字符
     */
    public function str_replace_once($needle, $replace, $haystack)
    
        $pos = strpos($haystack, $needle);
        if ($pos === false) 
            return $haystack;
        
        return substr_replace($haystack, $replace, $pos, strlen($needle));
    

8、随机字符

 /**
     * @param int $length 字符长度
     * @param $type 1 数字+字母 2数

以上是关于php开发常用的实用方法(第一节)(电商开发微信支付宝开发常用)的主要内容,如果未能解决你的问题,请参考以下文章

php开发微信APP支付接口

php开发微信图灵机器人

怎样用php开发微信小程序的后端?

php电商小程序的项目经验怎么写

PHP开发微信公众号二维码的获取

开发微信小程序的价值有哪些