当前位置:首页 > PHP教程 > php应用 > 列表

PHP随机生成中文段落示例【测试网站内容时使用】

发布:smiling 来源: PHP粉丝网  添加日期:2022-03-02 09:43:31 浏览: 评论:0 

本文实例讲述了PHP随机生成中文段落,分享给大家供大家参考,具体如下:

在创建网站的时候,很多时候都需要输入一些中文的段落来填充页面,大多数会是找一些新闻之类的来复制粘贴。

以下代码是利用php来随机生成一些中文段落,以便用来测试。

GBK版:

  1. <?php 
  2. header('Content-type:text/html;charset=gbk'); 
  3.  
  4. function createWords($words = 128) 
  5. { 
  6.   $seperate = array(",","。","!","?",";"); 
  7.   $strings = ''; 
  8.   for ($i=0; $i<$words; $i++) 
  9.   { 
  10.     $strings .= chr(rand(0xB0,0xD6)).chr(rand(0xA1,0xFE)); 
  11.     if (fmod($i, 18) > rand(10, 20)) 
  12.     { 
  13.       $strings .= $seperate[rand(0, 4)]; 
  14.     } 
  15.   } 
  16.   return $strings; 
  17. } 
  18.  
  19. $paras = rand(1, 10); 
  20.  
  21. $strings = ''; 
  22.  
  23. for($i=0; $i<$paras; $i++) 
  24. { 
  25.   $strings .= '&nbsp;&nbsp;&nbsp;&nbsp;'.createWords(rand(100, 500)).'<br />'; 
  26. } 
  27. ?> 
  28. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  29. <html> 
  30. <head> 
  31.   <meta http-equiv="Content-Type" content="text/html; charset=gbk" /> 
  32.   <title>随机生成中文段落</title> 
  33.   <style type="text/css"> 
  34.     body{ 
  35.       width: 960px; 
  36.       margin: 0 auto; 
  37.     } 
  38.     h1 { 
  39.       text-align: center; 
  40.     } 
  41.   </style> 
  42. </head> 
  43.  
  44. <body> 
  45.   <h1>刷新页面可得到不同结果</h1> 
  46.   <div><?php echo $strings;?></div> 
  47. </body> 

UTF8版:

  1. <?php 
  2. function createWords($words = 128) 
  3. { 
  4.   $seperate = array(",", "。", "!", "?", ";"); 
  5.   $strings = ''; 
  6.   for ($i=0; $i<128; $i++) 
  7.   { 
  8.     $strings .= iconv('utf-16', 'utf-8', chr(rand(0x00, 0xFF)).chr(rand(0x4E, 0x99))); 
  9.     if (fmod($i, 18) > rand(10, 20)) 
  10.     { 
  11.       $strings .= $seperate[rand(0, 4)]; 
  12.     } 
  13.   } 
  14.   return $strings; 
  15. } 
  16.  
  17. $paras = rand(1, 10); 
  18. $strings = ''; 
  19.  
  20. for($i=0; $i<$paras; $i++) 
  21. { 
  22.   $strings .= '&nbsp;&nbsp;&nbsp;&nbsp;'.createWords(rand(100, 500)).'<br />'; 
  23. } 
  24. ?> 
  25. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  26. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
  27. <head> 
  28.   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  29.   <title>测试</title> 
  30.   <style type="text/css"> 
  31.     body{ 
  32.       width: 960px; 
  33.       margin: 0 auto; 
  34.     } 
  35.     h1 { 
  36.       text-align: center; 
  37.     } 
  38.   </style> 
  39. </head> 
  40. <body> 
  41.   <h1>刷新页面可得到不同结果</h1> 
  42.   <div><?php echo $strings;?></div> 
  43. </body> 
  44. </html>

Tags: PHP随机生成中文段落

分享到: