Posted By


Biocrash on 07/13/12

Tagged


Statistics


Viewed 1183 times
Favorited by 0 user(s)

Lorem Ipsum Generator


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. if(!defined('BASEPATH'))
  3. exit('No direct script access allowed');
  4.  
  5. class LoremIpsumGenerator {
  6. /**
  7. * Copyright (c) 2009, Mathew Tinsley ([email protected])
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * * Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * * Neither the name of the organization nor the
  18. * names of its contributors may be used to endorse or promote products
  19. * derived from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY MATHEW TINSLEY ''AS IS'' AND ANY
  22. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24. * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
  25. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  26. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  28. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32.  
  33. private $words, $wordsPerParagraph, $wordsPerSentence;
  34.  
  35. function __construct($wordsPer = 100)
  36. {
  37. $this->wordsPerParagraph = $wordsPer;
  38. $this->wordsPerSentence = 24.460;
  39. $this->words = array(
  40. 'lorem',
  41. 'ipsum',
  42. 'dolor',
  43. 'sit',
  44. 'amet',
  45. 'consectetur',
  46. 'adipiscing',
  47. 'elit',
  48. 'curabitur',
  49. 'vel',
  50. 'hendrerit',
  51. 'libero',
  52. 'eleifend',
  53. 'blandit',
  54. 'nunc',
  55. 'ornare',
  56. 'odio',
  57. 'ut',
  58. 'orci',
  59. 'gravida',
  60. 'imperdiet',
  61. 'nullam',
  62. 'purus',
  63. 'lacinia',
  64. 'a',
  65. 'pretium',
  66. 'quis',
  67. 'congue',
  68. 'praesent',
  69. 'sagittis',
  70. 'laoreet',
  71. 'auctor',
  72. 'mauris',
  73. 'non',
  74. 'velit',
  75. 'eros',
  76. 'dictum',
  77. 'proin',
  78. 'accumsan',
  79. 'sapien',
  80. 'nec',
  81. 'massa',
  82. 'volutpat',
  83. 'venenatis',
  84. 'sed',
  85. 'eu',
  86. 'molestie',
  87. 'lacus',
  88. 'quisque',
  89. 'porttitor',
  90. 'ligula',
  91. 'dui',
  92. 'mollis',
  93. 'tempus',
  94. 'at',
  95. 'magna',
  96. 'vestibulum',
  97. 'turpis',
  98. 'ac',
  99. 'diam',
  100. 'tincidunt',
  101. 'id',
  102. 'condimentum',
  103. 'enim',
  104. 'sodales',
  105. 'in',
  106. 'hac',
  107. 'habitasse',
  108. 'platea',
  109. 'dictumst',
  110. 'aenean',
  111. 'neque',
  112. 'fusce',
  113. 'augue',
  114. 'leo',
  115. 'eget',
  116. 'semper',
  117. 'mattis',
  118. 'tortor',
  119. 'scelerisque',
  120. 'nulla',
  121. 'interdum',
  122. 'tellus',
  123. 'malesuada',
  124. 'rhoncus',
  125. 'porta',
  126. 'sem',
  127. 'aliquet',
  128. 'et',
  129. 'nam',
  130. 'suspendisse',
  131. 'potenti',
  132. 'vivamus',
  133. 'luctus',
  134. 'fringilla',
  135. 'erat',
  136. 'donec',
  137. 'justo',
  138. 'vehicula',
  139. 'ultricies',
  140. 'varius',
  141. 'ante',
  142. 'primis',
  143. 'faucibus',
  144. 'ultrices',
  145. 'posuere',
  146. 'cubilia',
  147. 'curae',
  148. 'etiam',
  149. 'cursus',
  150. 'aliquam',
  151. 'quam',
  152. 'dapibus',
  153. 'nisl',
  154. 'feugiat',
  155. 'egestas',
  156. 'class',
  157. 'aptent',
  158. 'taciti',
  159. 'sociosqu',
  160. 'ad',
  161. 'litora',
  162. 'torquent',
  163. 'per',
  164. 'conubia',
  165. 'nostra',
  166. 'inceptos',
  167. 'himenaeos',
  168. 'phasellus',
  169. 'nibh',
  170. 'pulvinar',
  171. 'vitae',
  172. 'urna',
  173. 'iaculis',
  174. 'lobortis',
  175. 'nisi',
  176. 'viverra',
  177. 'arcu',
  178. 'morbi',
  179. 'pellentesque',
  180. 'metus',
  181. 'commodo',
  182. 'ut',
  183. 'facilisis',
  184. 'felis',
  185. 'tristique',
  186. 'ullamcorper',
  187. 'placerat',
  188. 'aenean',
  189. 'convallis',
  190. 'sollicitudin',
  191. 'integer',
  192. 'rutrum',
  193. 'duis',
  194. 'est',
  195. 'etiam',
  196. 'bibendum',
  197. 'donec',
  198. 'pharetra',
  199. 'vulputate',
  200. 'maecenas',
  201. 'mi',
  202. 'fermentum',
  203. 'consequat',
  204. 'suscipit',
  205. 'aliquam',
  206. 'habitant',
  207. 'senectus',
  208. 'netus',
  209. 'fames',
  210. 'quisque',
  211. 'euismod',
  212. 'curabitur',
  213. 'lectus',
  214. 'elementum',
  215. 'tempor',
  216. 'risus',
  217. 'cras' );
  218. }
  219.  
  220. function getContent($count, $format = 'html', $loremipsum = true)
  221. {
  222. $format = strtolower($format);
  223.  
  224. if($count <= 0)
  225. return '';
  226.  
  227. switch($format)
  228. {
  229. case 'txt':
  230. return $this->getText($count, $loremipsum);
  231. case 'plain':
  232. return $this->getPlain($count, $loremipsum);
  233. default:
  234. return $this->getHTML($count, $loremipsum);
  235. }
  236. }
  237.  
  238. private function getWords(&$arr, $count, $loremipsum)
  239. {
  240. $i = 0;
  241. if($loremipsum)
  242. {
  243. $i = 2;
  244. $arr[0] = 'lorem';
  245. $arr[1] = 'ipsum';
  246. }
  247.  
  248. for($i; $i < $count; $i++)
  249. {
  250. $index = array_rand($this->words);
  251. $word = $this->words[$index];
  252. //echo $index . '=>' . $word . '<br />';
  253.  
  254. if($i > 0 && $arr[$i - 1] == $word)
  255. $i--;
  256. else
  257. $arr[$i] = $word;
  258. }
  259. }
  260.  
  261. private function getPlain($count, $loremipsum, $returnStr = true)
  262. {
  263. $words = array();
  264. $this->getWords($words, $count, $loremipsum);
  265. //print_r($words);
  266.  
  267. $delta = $count;
  268. $curr = 0;
  269. $sentences = array();
  270. while($delta > 0)
  271. {
  272. $senSize = $this->gaussianSentence();
  273. //echo $curr . '<br />';
  274. if(($delta - $senSize) < 4)
  275. $senSize = $delta;
  276.  
  277. $delta -= $senSize;
  278.  
  279. $sentence = array();
  280. for($i = $curr; $i < ($curr + $senSize); $i++)
  281. $sentence[] = $words[$i];
  282.  
  283. $this->punctuate($sentence);
  284. $curr = $curr + $senSize;
  285. $sentences[] = $sentence;
  286. }
  287.  
  288. if($returnStr)
  289. {
  290. $output = '';
  291. foreach($sentences as $s)
  292. foreach($s as $w)
  293. $output .= $w . ' ';
  294.  
  295. return $output;
  296. }
  297. else
  298. return $sentences;
  299. }
  300.  
  301. private function getText($count, $loremipsum)
  302. {
  303. $sentences = $this->getPlain($count, $loremipsum, false);
  304. $paragraphs = $this->getParagraphArr($sentences);
  305.  
  306. $paragraphStr = array();
  307. foreach($paragraphs as $p)
  308. {
  309. $paragraphStr[] = $this->paragraphToString($p);
  310. }
  311.  
  312. $paragraphStr[0] = "\t" . $paragraphStr[0];
  313. return implode("\n\n\t", $paragraphStr);
  314. }
  315.  
  316. private function getParagraphArr($sentences)
  317. {
  318. $wordsPer = $this->wordsPerParagraph;
  319. $sentenceAvg = $this->wordsPerSentence;
  320. $total = count($sentences);
  321.  
  322. $paragraphs = array();
  323. $pCount = 0;
  324. $currCount = 0;
  325. $curr = array();
  326.  
  327. for($i = 0; $i < $total; $i++)
  328. {
  329. $s = $sentences[$i];
  330. $currCount += count($s);
  331. $curr[] = $s;
  332. if($currCount >= ($wordsPer - round($sentenceAvg / 2.00)) || $i == $total - 1)
  333. {
  334. $currCount = 0;
  335. $paragraphs[] = $curr;
  336. $curr = array();
  337. //print_r($paragraphs);
  338. }
  339. //print_r($paragraphs);
  340. }
  341.  
  342. return $paragraphs;
  343. }
  344.  
  345. private function getHTML($count, $loremipsum)
  346. {
  347. $sentences = $this->getPlain($count, $loremipsum, false);
  348. $paragraphs = $this->getParagraphArr($sentences);
  349. //print_r($paragraphs);
  350.  
  351. $paragraphStr = array();
  352. foreach($paragraphs as $p)
  353. {
  354. $paragraphStr[] = "<p>\n" . $this->paragraphToString($p, true) . '</p>';
  355. }
  356.  
  357. //add new lines for the sake of clean code
  358. return implode("\n", $paragraphStr);
  359. }
  360.  
  361. private function paragraphToString($paragraph, $htmlCleanCode = false)
  362. {
  363. $paragraphStr = '';
  364. foreach($paragraph as $sentence)
  365. {
  366. foreach($sentence as $word)
  367. $paragraphStr .= $word . ' ';
  368.  
  369. if($htmlCleanCode)
  370. $paragraphStr .= "\n";
  371. }
  372. return $paragraphStr;
  373. }
  374.  
  375. /*
  376. * Inserts commas and periods in the given
  377. * word array.
  378. */
  379. private function punctuate(& $sentence)
  380. {
  381. $count = count($sentence);
  382. $sentence[$count - 1] = $sentence[$count - 1] . '.';
  383.  
  384. if($count < 4)
  385. return $sentence;
  386.  
  387. $commas = $this->numberOfCommas($count);
  388.  
  389. for($i = 1; $i <= $commas; $i++)
  390. {
  391. $index = (int) round($i * $count / ($commas + 1));
  392.  
  393. if($index < ($count - 1) && $index > 0)
  394. {
  395. $sentence[$index] = $sentence[$index] . ',';
  396. }
  397. $sentence[0] = ucfirst($sentence[0]); //capitalize the first word
  398. }
  399. }
  400.  
  401. /*
  402. * Determines the number of commas for a
  403. * sentence of the given length. Average and
  404. * standard deviation are determined superficially
  405. */
  406. private function numberOfCommas($len)
  407. {
  408. $avg = (float) log($len, 6);
  409. $stdDev = (float) $avg / 6.000;
  410.  
  411. return (int) round($this->gauss_ms($avg, $stdDev));
  412. }
  413.  
  414. /*
  415. * Returns a number on a gaussian distribution
  416. * based on the average word length of an english
  417. * sentence.
  418. * Statistics Source:
  419. * http://hearle.nahoo.net/Academic/Maths/Sentence.html
  420. * Average: 24.46
  421. * Standard Deviation: 5.08
  422. */
  423. private function gaussianSentence()
  424. {
  425. $avg = (float) 24.460;
  426. $stdDev = (float) 5.080;
  427.  
  428. return (int) round($this->gauss_ms($avg, $stdDev));
  429. }
  430.  
  431. /*
  432. * The following three functions are used to
  433. * compute numbers with a guassian distrobution
  434. * Source:
  435. * http://us.php.net/manual/en/function.rand.php#53784
  436. */
  437. private function gauss()
  438. { // N(0,1)
  439. // returns random number with normal distribution:
  440. // mean=0
  441. // std dev=1
  442.  
  443. // auxilary vars
  444. $x=$this->random_0_1();
  445. $y=$this->random_0_1();
  446.  
  447. // two independent variables with normal distribution N(0,1)
  448. $u=sqrt(-2*log($x))*cos(2*pi()*$y);
  449. $v=sqrt(-2*log($x))*sin(2*pi()*$y);
  450.  
  451. // i will return only one, couse only one needed
  452. return $u;
  453. }
  454.  
  455. private function gauss_ms($m=0.0,$s=1.0)
  456. {
  457. return $this->gauss()*$s+$m;
  458. }
  459.  
  460. private function random_0_1()
  461. {
  462. return (float)rand()/(float)getrandmax();
  463. }
  464.  
  465. }
  466.  

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.