提供测试数据类

提供测试数据类

在开发过程中,我们有可能对数据库要输入测试数据,这里封装了一些
提供简单格式的测试数据类

代码

<?php
header('content-type:text/html;charset=utf-8');
//整合需要插入的资源
class OperateResource
{ 
    public $chinese;//提供中文字符的资源
    public $chinese_arr;
    public function __construct($chinese)
    { 
        $this->chinese = $chinese;
        $this->explodeCN();
    }
    //中文字符串单个字符以索引数组形式给出
    public function explodeCN()
    { 
        $length = mb_strlen($this->chinese,'utf-8');

        $array = array();
        for($i=0;$i<$length;$i++){ 
            $array[] = mb_substr($this->chinese,$i,1,'utf-8');
        }
        $this->chinese_arr = $array;
    }
    //随机输出用户姓名
    public function outputName()
    { 
        //数组的元素值
        $arr_num = count($this->chinese_arr);
        $num_for = rand(2,4);
        $str = "";
        for($i=0;$i<$num_for;$i++){ 
            $num_random = rand(0,$arr_num);
            $str .= $this->chinese_arr[$num_random]; 
        }
        return $str;
    }
    //随机输出一定范围的文字
    public function outputText($min_num,$max_num)
    { 
        $arr_num = count($this->chinese_arr);
        $num_for = rand($min_num,$max_num);
        $text = "";
        for($i=0;$i<$num_for;$i++){ 
            $num_random = rand(0,$arr_num);
            $text .= $this->chinese_arr[$num_random];
        }
        return $text;
    }
    //随机输出一个电话号码
    public function outputtel()
    { 
        $first = 1;

        $second_arr = array(3,5,8);
        $second_key = array_rand($second_arr);
        $second = $second_arr[$second_key];

        $other = "";
        for($i=0;$i<9;$i++){ 
            $other .= rand(0,9);
        }
        $tel = $first.$second.$other;
        return $tel;
    }
    //随机输出字母数字
    public function num_str($min,$max)
    { 
        $str = "";
        for($i=0;$i<rand($min,$max);$i++){ 
            $handle = rand(0,2);
            if(0 == $handle){ 
                $str .= chr(rand(97,122));
            }elseif(1 == $handle){ 
                $str .= chr(rand(65,90));
            }else{ 
                $str .= rand(0,9);
            }
        }
        return $str;
    }
    //随机输出一个邮箱
    public function outmail()
    { 
        $str1 = $this->num_str(8,16);
        $str2 = $this->num_str(2,5);
        $email = $str1.'@'.$str2.".com";
        return $email;
    } 

}

你可能感兴趣的:(PHP)