本文實(shí)例講述了PHP隨機(jī)生成中文段落。分享給大家供大家參考,具體如下:
在創(chuàng)建網(wǎng)站的時(shí)候,很多時(shí)候都需要輸入一些中文的段落來填充頁(yè)面,大多數(shù)會(huì)是找一些新聞之類的來復(fù)制粘貼。
以下代碼是利用php來隨機(jī)生成一些中文段落,以便用來測(cè)試。
GBK版:
?php
header('Content-type:text/html;charset=gbk');
function createWords($words = 128)
{
$seperate = array(",","。","!","?",";");
$strings = '';
for ($i=0; $i$words; $i++)
{
$strings .= chr(rand(0xB0,0xD6)).chr(rand(0xA1,0xFE));
if (fmod($i, 18) > rand(10, 20))
{
$strings .= $seperate[rand(0, 4)];
}
}
return $strings;
}
$paras = rand(1, 10);
$strings = '';
for($i=0; $i$paras; $i++)
{
$strings .= 'nbsp;nbsp;nbsp;nbsp;'.createWords(rand(100, 500)).'br />';
}
?>
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
html>
head>
meta http-equiv="Content-Type" content="text/html; charset=gbk" />
title>隨機(jī)生成中文段落/title>
style type="text/css">
body{
width: 960px;
margin: 0 auto;
}
h1 {
text-align: center;
}
/style>
/head>
body>
h1>刷新頁(yè)面可得到不同結(jié)果/h1>
div>?php echo $strings;?>/div>
/body>
UTF8版:
?php
function createWords($words = 128)
{
$seperate = array(",", "。", "!", "?", ";");
$strings = '';
for ($i=0; $i128; $i++)
{
$strings .= iconv('utf-16', 'utf-8', chr(rand(0x00, 0xFF)).chr(rand(0x4E, 0x99)));
if (fmod($i, 18) > rand(10, 20))
{
$strings .= $seperate[rand(0, 4)];
}
}
return $strings;
}
$paras = rand(1, 10);
$strings = '';
for($i=0; $i$paras; $i++)
{
$strings .= 'nbsp;nbsp;nbsp;nbsp;'.createWords(rand(100, 500)).'br />';
}
?>
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
head>
meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
title>測(cè)試/title>
style type="text/css">
body{
width: 960px;
margin: 0 auto;
}
h1 {
text-align: center;
}
/style>
/head>
body>
h1>刷新頁(yè)面可得到不同結(jié)果/h1>
div>?php echo $strings;?>/div>
/body>
/html>
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP編碼與轉(zhuǎn)碼操作技巧匯總》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php常用函數(shù)與技巧總結(jié)》及《PHP錯(cuò)誤與異常處理方法總結(jié)》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:- PHP生成隨機(jī)字符串(3種方法)
- PHP生成不重復(fù)隨機(jī)數(shù)的方法匯總
- PHP生成指定長(zhǎng)度隨機(jī)數(shù)最簡(jiǎn)潔的方法
- php隨機(jī)生成數(shù)字字母組合的方法
- PHP生成隨機(jī)用戶名和密碼的實(shí)現(xiàn)代碼
- PHP 生成N個(gè)不重復(fù)的隨機(jī)數(shù)
- 使用PHP批量生成隨機(jī)用戶名
- php簡(jiǎn)單生成隨機(jī)顏色的方法
- php隨機(jī)輸出名人名言的代碼
- PHP實(shí)現(xiàn)的英文名字全拼隨機(jī)排號(hào)腳本
- PHP基于自定義類隨機(jī)生成姓名的方法示例