Lorem Ipsum Dolor – Generator of Random Words and Phrases in PHP

Lorem ipsum or just Lipsum is placeholder text commonly used in laying out web designs, print or graphic.

For those who need such text generated automatically and randomly for the content of templates in web design, here is a class in php that offers two functions to generate both whole sentences and paragraphs.

/**
 * Lorem Ipsum Dolor Generator: __Lipsum Class
 *
 * PHP version 5.3+
 *
 * Licensed under The MIT License.
 * Redistribution of these files must retain the above copyright notice.
 *
 * @author    Emilian Robert Vicol <byrev[*]yahoo.com>
 * @copyright Copyright 2020+ Emilian Robert Vicol
 * @license   https://opensource.org/licenses/mit-license.html
 * @link      https://robertvicol.com/
 * @version   1.0
 */
abstract class __Lipsum {
    public static function LoremW($n1,$n2, $echo=false, $comma=false) {
    $len= count(self::$lorem)-1;
    $n = random_int($n1, $n2);				
    $ncomma = $comma && ($n>3) && (random_int(1,2)==2) ? random_int(0, $n-3) : $n;	
    
    $words = [];
    for ($i=0; $i<$n; $i++) {
      $w = self::$lorem[random_int(0, $len)];
      if ($i == $ncomma) 
        $w .= ',';
      $words[] = $w;
    }

    $result = ucfirst(implode(' ', $words));	
    if (!$echo) 		
      return $result;		
    echo $result;		
  }
  
  public static function LoremPh ($nparagraph, $echo=false, $separator="\n\n", $nsentences=[3,7], $nwords = [3,17]) {		
    if (is_array($nparagraph)) 
      $nparagraph = random_int($nparagraph[0], $nparagraph[1]);	

    $paragraph = [];
    for ($np=0; $np < $nparagraph; $np++) {
      $ns_rand= random_int($nsentences[0], $nsentences[1]);
      $sentences = [];
      for ($ns=0; $ns < $ns_rand; $ns++) {			
        $sentences[] = self::LoremW($nwords[0], $nwords[1], false, true).'.';
      }
      $paragraphs[] = implode(' ', $sentences);
    }
    
    $result = implode($separator, $paragraphs);		
    if (!$echo) 		
      return $result;		
    echo $result;
  }	

    private static $lorem = ['dapibus', 'aliquam', 'placerat', 'scelerisque', 'sit', 'dolor', 'eu', 'habitasse', 'habitant', 'ullamcorper', 'vehicula', 'phasellus', 'pariatur', 'tempor', 'excepteur', 'posuere', 'primis', 
  'in', 'fames', 'mauris', 'porttitor', 'nulla', 'suscipit', 'laboris', 'duis', 'dolore', 'lorem', 'inceptos', 'metus', 'accumsan', 'vel', 'platea', 'in', 'amet', 'sit', 'turpis', 'ex', 'mattis', 'litora', 
  'ipsum', 'ipsum', 'ad', 'elementum', 'semper', 'in', 'consectetur', 'faucibus', 'nisl', 'netus', 'varius', 'tempus', 'suspendisse', 'iaculis', 'eu', 'id', 'voluptate', 'quis', 'id', 'quisque', 'velit', 
  'dignissim', 'cras', 'dictumst', 'fringilla', 'molestie', 'aliqua', 'efficitur', 'incididunt', 'labore', 'fermentum', 'curae', 'aliquip', 'nullam', 'esse', 'mollis', 'ante', 'donec', 'purus', 'integer', 
  'interdum', 'arcu', 'enim', 'ultrices', 'adipiscing', 'sapien', 'imperdiet', 'potenti', 'cillum', 'elit', 'porta', 'justo', 'lorem', 'sed', 'feugiat', 'fusce', 'ligula', 'ut', 'curabitur', 'sodales', 'nibh', 
  'euismod', 'vitae', 'lacus', 'neque', 'malesuada', 'ut', 'orci', 'proident', 'luctus', 'eros', 'conubia', 'praesent', 'rhoncus', 'laborum', 'dolor', 'lectus', 'per', 'urna', 'nisi', 'aute', 'qui', 'culpa', 
  'deserunt', 'class', 'pulvinar', 'ex', 'nostra', 'himenaeos', 'ac', 'tortor', 'consectetur', 'et', 'gravida', 'quam', 'taciti', 'ornare', 'egestas', 'risus', 'condimentum', 'commodo', 'magna', 'ut', 
  'lacinia', 'venenatis', 'est', 'sociosqu', 'et', 'diam', 'irure', 'cubilia', 'torquent', 'vestibulum', 'consequat', 'aptent', 'blandit', 'non', 'pharetra', 'eiusmod', 'sunt', 'dui', 'tincidunt', 'cupidatat', 
  'occaecat', 'in', 'hendrerit', 'consequat', 'leo', 'elit', 'sed', 'sem', 'sagittis', 'do', 'commodo', 'mollit', 'congue', 'facilisis', 'etiam', 'aenean', 'at', 'morbi', 'sint', 'pellentesque', 'velit', 
  'ut', 'dolore', 'finibus', 'volutpat', 'enim', 'erat', 'pretium', 'amet', 'felis', 'laoreet', 'viverra', 'est', 'ad', 'dolor', 'senectus', 'exercitation', 'magna', 'convallis', 'nam', 'duis', 'proin', 
  'lobortis', 'a', 'eget', 'mi', 'minim', 'hac', 'nulla', 'veniam', 'tellus', 'anim', 'maximus', 'maecenas', 'quis', 'nec', 'non', 'adipiscing', 'nunc', 'officia', 'auctor', 'ullamco', 'fugiat', 
  'sollicitudin', 'ea', 'reprehenderit', 'eleifend', 'nisi', 'tempor', 'vivamus', 'libero', 'odio', 'tristique', 'cursus', 'massa', 'aliquet', 'augue', 'dictum', 'ultricies', 'nostrud', 'bibendum', 'vulputate', ];
}

Example of using the __Lipsum class

1) generate 3 to 5 paragraphs (randomly), no echo, custom separator, with default other parameters: random (3 to 7) number of sentences and each sentence with random (3 to 17) words  :

$lipsumvar = __Lipsum::LoremPh([3,5],false,"<br /><br />\n\n");

Elit exercitation ultricies consequat voluptate euismod suspendisse fames id fames elit amet ex proident feugiat nibh nostra. Est, officia leo sagittis. Pulvinar, leo nibh gravida rhoncus ornare ipsum sint enim dolore nostra torquent litora. Ipsum tempus voluptate dignissim nostra ac congue porta excepteur quisque integer veniam primis. Lobortis nulla cubilia ornare in. Velit habitasse hendrerit id vivamus sed in volutpat lectus per tellus molestie eiusmod lectus.

Conubia morbi ligula. Vehicula ut sit class lobortis pariatur mi elit velit molestie, sollicitudin proin nunc potenti fusce feugiat anim. Lorem exercitation diam placerat do tempor elit id velit id non fringilla. Mollis sed per eiusmod aliquip, adipiscing ornare duis euismod. Porttitor eu, suscipit dolor laoreet dapibus consequat a. Massa faucibus consectetur esse elit justo tristique veniam pulvinar posuere.

Hac pellentesque eiusmod ad ut id ornare porttitor, condimentum sit dolor orci tristique massa ut nec in. Leo dolore porttitor excepteur scelerisque cras, pellentesque massa scelerisque fugiat dapibus ex mi. Viverra efficitur ultricies adipiscing lobortis ut ultricies proin maecenas ea aliquip quis sociosqu elit ultricies est cupidatat. Risus id leo neque sunt porta sociosqu posuere suscipit ipsum ut ullamcorper gravida sit.

At facilisis dolor hendrerit esse nostrud irure culpa platea sodales. Consectetur viverra odio, in quis leo hac himenaeos dolor. Elit tempor massa ultrices malesuada. Ut sed nisl hac, vestibulum sint netus dui. Duis nec eleifend. Maximus arcu aliquip potenti sunt dolor dolor libero ultrices nulla orci. Tempor netus dictum aliqua turpis, posuere sodales quisque in nulla.

Vehicula non venenatis. Est lorem, tincidunt interdum ex lacus in euismod vestibulum urna. Curabitur proident porttitor. Aenean hendrerit luctus litora veniam sit velit, at nec.

2)  generate 1 paragraphs with 2 sentences and 7 words each sentence, then displays on the web page:

__Lipsum::LoremPh(1, true, "",[2,2],[7,7]);

Senectus, nostrud curabitur per labore anim cursus. Officia, lobortis interdum anim porta eu bibendum.

3) Generates a sentence with a number of 3 to 5 words, no echo, which may (or may not) contain a comma:

$lipsumvar = __Lipsum::LoremW(3,5, false, true);

Dolor mattis ea velit officia proident

byrev Written by:

Be First to Comment

    Leave a Reply

    Your email address will not be published. Required fields are marked *