标签:
在工作中,我们有时候需要生成一些随机的中文,下面总结一个方法,供大家参考。
//原始文本文件 mytext.php; <?php return ‘开放日入驻大学孵化器的创业公司会参与展会同时主办方邀请创业圈内知名的企业家进行脱口秀交流活动年轻人们‘; //将文本文件的中文转华成数组写入文件 start.php $wenku = @include ‘http://www.onesheng.cn‘; $wenku = iconv("utf-8","utf-8",$wenku); $zhongwenku_size = mb_strlen($wenku,"utf-8"); $zhongwenku = array(); for( $i=0; $i<$zhongwenku_size; $i++) { $zhongwenku[$i] = mb_substr($wenku, $i,1,"utf-8"); } $filename="mytextarray.php"; $return = var_export($zhongwenku, TRUE); // if (file_put_contents($filename, "<?php \r\n return " . $return . ";")) { echo ‘ok‘; } else { echo ‘no‘; } //下面获取中文随机数 index.php $wenku = @include ‘mytextarray.php‘; $size = count($wenku); echo getzhongwen($wenku,$size); function getzhongwen($wenku,$size){ $zhongwen = ‘‘; $icount = mt_rand(5,15); for($i = 0;$i<$icount;$i++){ $sum = mt_rand(0,$size); $zhongwen .= $wenku[$sum]; } return $zhongwen; }
标签:
原文地址:http://www.cnblogs.com/phplover2014/p/4189642.html