hhhzihao2
2016-10-05 16:08:42浏览 5910
<?php
/* 设置页面内容是html编码格式 */
header('Content-Type:text/html; charset=utf-8');
/**
* 生成32位的唯一字符串
*/
function unique_name() {
return md5(uniqid(mt_rand(), true));
}
/**
* 生成毫/微秒的时间戳
*/
function microtime_float() {
/* 微秒 */
list($usec, $sec) = explode(' ', microtime());
/* 非科学计数法 */
return array('ms'=>microtime(true)*10000, 'mi'=>sprintf("%.0f", ((float)$usec + (float)$sec)*100000000), );
}
/**
* 生成指定位数的随机数
*/
function random_number( $a = array('can_zero_start'=>null,'length'=>null,'prefix'=>null,'suffix'=>null,) ){
/* 允许0开头 */
$can_zero_start = isset($a['can_zero_start']) ? $a['can_zero_start'] : true;
if ( $can_zero_start ) {
$num = mt_rand( 0, 9 );
} else {
$num = mt_rand( 1, 9 );
}
/* 长度 */
$length = isset($a['length']) ? $a['length'] : 8;
for ( $i = 0 ; $i < $length-1 ; $i++ ) {
$num .= mt_rand( 0, 9 );
}
/* 前缀 */
$prefix = isset($a['prefix']) ? $a['prefix'] : null;
/* 后缀 */
$suffix = isset($a['suffix']) ? $a['suffix'] : null;
return $prefix . $num . $suffix;
}
/**
* 生成验证码
*/
function verification_code( $a = array('length'=>null,'number_case'=>null,'lower_case'=>null,'upper_case'=>null,'chinese'=>null,) ) {
/* 字符库 */
$number_str_all = '0123456789';
$number_str = '23456789';
$lower_case_str = 'abcdefghijkmnpqrstuvwxyz';
$upper_case_str = 'ABCDEFGHJKLMNPQRSTUVWXYZ';
$chinese_library = chinese_library();
/* 含有数字 */
$number_case = isset($a['number_case']) ? $a['number_case'] : true;
/* 含有小写字母 */
$lower_case = isset($a['lower_case']) ? $a['lower_case'] : true;
/* 含有大写字母 */
$upper_case = isset($a['upper_case']) ? $a['upper_case'] : true;
/* 中文 */
$chinese = isset($a['chinese']) ? $a['chinese'] : false;
/* 拼接字符库 */
if ( $chinese or ( $number_case == false and $lower_case == false and $upper_case == false ) ) {
$str = $chinese_library['str'];
} elseif ( $number_case == true and $lower_case == false and $upper_case == false ) {
$str = $number_str_all;
} elseif ( $number_case != true or $lower_case != false or $upper_case != false ) {
$str = null;
if ( $number_case ) $str .= $number_str;
if ( $lower_case ) $str .= $lower_case_str;
if ( $upper_case ) $str .= $upper_case_str;
}
/* 长度 */
$length = isset($a['length']) ? $a['length'] : 4;
/* 拼接验证码 */
$array_rand_val = array_rand_val( array('data'=>$str,'length'=>$length,'repeat'=>true,) );
return array('code'=>$array_rand_val['str'],'code_arr'=>$array_rand_val['arr'],);
}
/**
* 生成中文验证码(包括候选码)
*/
function chinese_code( $a = array('length_code'=>null,'length_candidate'=>null,) ) {
/* 验证码长度 */
$length_code = isset($a['length_code']) ? $a['length_code'] : 2;
/* 候选码长度 */
$length_candidate = isset($a['length_candidate']) ? $a['length_candidate'] : 9;
/* 如果填反了 */
if ( $length_code > $length_candidate ) {
list($length_code, $length_candidate) = array($length_candidate, $length_code);
}
/* 生成候选码 */
$chinese_library = chinese_library();
$candidate = array_rand_val( array('data'=>$chinese_library['arr'], 'length'=>$length_candidate, 'repeat'=>false, ) );
/* 生成验证码 */
$code = array_rand_val( array('data'=>$candidate['arr'],'length'=>$length_code,'repeat'=>false,) );
return array('code_str'=>$code['str'], 'candidate_str'=>$candidate['str'], 'code_arr'=>$code['arr'], 'candidate_arr'=>$candidate['arr'], );
}
/**
* 汉字库
*/
function chinese_library() {
/* 字符串 */
$str = '的一是了不人在他有这个上们来到时大地为子中你说生国年着就那和要她出也得里后自以会家可下而过天去能对小多然于心学么之都好看起发当没成只如'
.'事把还用第样道想作种开美总从无情己面最女但现前些所同手又行意动方期它头经长儿回位分爱老因很给名法间斯知世什两次使身者被高已亲其进此话常与活正感';
/* 数组 */
preg_match_all('/./u',$str,$arr);
/* 返回 字符串 & 数组 */
return array( 'arr'=>$arr[0], 'str'=>$str,);
}
/**
* 随机获取 数组/字符串 中的值/字符
*/
function array_rand_val( $a = array('data'=>null,'length'=>null,'repeat'=>null,) ) {
/* 转数组 */
if ( !isset($a['data']) ) {
$arr = array(1,2,3);
} elseif ( !is_array($a['data']) ) {
preg_match_all('/\S/u', $a['data'], $b);
$arr = $b[0];
} elseif ( is_array($a['data']) ) {
$arr = $a['data'];
}
/* 可重复吗 */
$repeat = isset($a['repeat']) ? $a['repeat'] : false;
/* 获取个数 */
$length = isset($a['length']) ? $a['length'] : 1;
if ( $repeat ) {
$return_arr = array();
for ( $i = 0; $i < $length; $i++ ) {
/* 随机键名的值 */
$return_arr[] = $arr[array_rand($arr)];
}
} elseif ( !$repeat ) {
/* 打乱 */
shuffle($arr);
/* 获取 */
$return_arr = array_slice($arr, 0, $length);
}
return array('arr'=>$return_arr, 'str'=>implode($return_arr), );
}
热门评论
#include<stdio.h>
int main()
{
printf("Very Good!");
}