標籤:
在工作中,我們有時候需要產生一些隨機的中文,下面總結一個方法,供大家參考。
//原始文字檔mytext.php;<?phpreturn ‘開放日入駐大學孵化器的創業公司會參與展會同時主辦方邀請創業圈內知名的企業家進行脫口秀交流活動年輕人們‘;//將文字檔的中文轉華成數組寫入檔案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;}
PHP抓取到中文文本並寫入資料庫作業碼