How to get random text from lorem Ipsum in PHP?

random text generator
lorem ipsum algorithm
lorem ipsum generators
lorem ipsum bullet points
lorem ipsum api
php random string
lorem ipsum rich text
lorem ipsum dolor sit amet, consectetur adipiscing elit

I want the random text from Lorem Ipsum so I can use it when generating webpages. I can't find any PHP functions that does this and I'm wondering if there's any publicly available libraries or APIs on sites that could be used to get some random text?

joshtronic/php-loremipsum: Lorem ipsum generator in PHP , Lorem ipsum generator in PHP without dependencies. #5 Generate random date and time outputs Opened by fernandoluiswendt about 1 month ago #4  This class can generate random text based on lorem ipsum template. It can generate a random text with a number of words between a given minimum and maximum range of words. The generated text is based on the lorem ipsum template but the base template text can be changed. The generate text may start from the beginning of the template or another

I didn't really want to make an API call to get some random text, so I wrote it myself:

<?php

abstract class Lorem {
    public static function ipsum($nparagraphs) {
        $paragraphs = [];
        for($p = 0; $p < $nparagraphs; ++$p) {
            $nsentences = random_int(3, 8);
            $sentences = [];
            for($s = 0; $s < $nsentences; ++$s) {
                $frags = [];
                $commaChance = .33;
                while(true) {
                    $nwords = random_int(3, 15);
                    $words = self::random_values(self::$lorem, $nwords);
                    $frags[] = implode(' ', $words);
                    if(self::random_float() >= $commaChance) {
                        break;
                    }
                    $commaChance /= 2;
                }

                $sentences[] = ucfirst(implode(', ', $frags)) . '.';
            }
            $paragraphs[] = implode(' ', $sentences);
        }
        return implode("\n\n", $paragraphs);
    }

    private static function random_float() {
        return random_int(0, PHP_INT_MAX - 1) / PHP_INT_MAX;
    }

    private static function random_values($arr, $count) {
        $keys = array_rand($arr, $count);
        if($count == 1) {
            $keys = [$keys];
        }
        return array_intersect_key($arr, array_fill_keys($keys, null));
    }

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

Usage:

Lorem::ipsum(5);

Sample output:

"""
Ligula ornare commodo elementum. Cursus gravida bibendum. Lacus integer auctor primis ad odio suscipit diam, justo feugiat hendrerit. Ipsum suspendisse nunc tempor ultrices orci odio potenti imperdiet netus fames. Viverra feugiat sollicitudin euismod commodo dui lectus magna sem aliquet cras, sit mattis vulputate enim eros. Interdum a primis orci arcu porttitor taciti. Maecenas mauris mollis purus nullam enim laoreet aliquet, maecenas feugiat auctor vulputate quam condimentum vehicula tristique. Consectetur mauris auctor sollicitudin.\n
\n
Sit at tincidunt lacinia nec tempus conubia bibendum fames. Justo massa odio. Sit praesent tincidunt nisi molestie cursus ornare tempus class duis, dolor vitae luctus eleifend ut curae hendrerit vulputate dui aptent litora suscipit dignissim, sed auctor ultricies dictumst vel. Etiam mattis a est phasellus felis posuere pharetra consequat platea taciti torquent neque nisl cras. Adipiscing in viverra a molestie fusce pretium tempus porta curabitur. Aptent risus nisl.\n
\n
Ipsum quisque auctor venenatis varius posuere hendrerit eu libero vel taciti ad conubia cras, placerat nec phasellus gravida aliquet. Dictum lacus leo facilisis ut quis gravida litora bibendum laoreet. Interdum tincidunt ac suspendisse venenatis nullam pretium hac himenaeos diam vehicula.\n
\n
Nulla eleifend varius hendrerit condimentum efficitur fames. Metus pulvinar pretium habitasse vivamus efficitur turpis porta rhoncus elementum netus. Mollis phasellus fusce nullam eu vel sodales duis.\n
\n
Ipsum egestas pulvinar tortor venenatis fusce consequat pellentesque class fermentum potenti suscipit diam tristique. Sit tellus magna. Lobortis venenatis quis nisi condimentum nostra ullamcorper.
"""

This depends on PHP 7's CSPRNG functions, which you can either polyfill or sub out for mt_rand if you're on PHP 5.x.

PHP Lipsum Generator: Generate random text based on lorem , This class can generate random text based on lorem ipsum template. It can generate a random text with a number of words between a given minimum and  You can generate random Lorem Ipsum text from command line outside web server context using CLI script find into bin/lorem. An help is available by calling bin/lorem --help . But it is easy to use it, for example, getting 5 paragraphs is bin/lorem --paragraph=5 or bin/lorem -p=5 .

$lipsum = simplexml_load_file('http://www.lipsum.com/feed/xml?amount=1&what=paras&start=0')->lipsum;

OR you can make a function:

function random_lipsum($amount = 1, $what = 'paras', $start = 0) {
    return simplexml_load_file("http://www.lipsum.com/feed/xml?amount=$amount&what=$what&start=$start")->lipsum;
}

Source

PHP Lorem-Ipsum Generator Class: Generate fake text to test page , It can generate text using random words read from a sample text file. The number of generated words is configurable. The output text may be formatted as multi-  Dummy text generator. Lorem Ipsum is a dummy text random generator. You choose the number of paragraphs, words or lists. You then get a random text that you can then freely use it in your layout. The generated text is the Latin nickname and may give the impression of being the real text. Generate paragraphs:

http://www.lipsum.com/

for php use:

$lipsum = simplexml_load_file('http://www.lipsum.com/feed/xml?amount=1&what=paras&start=0')->lipsum;

or in a function:

function random_lipsum($amount = 1, $what = 'paras', $start = 0) {
return simplexml_load_file("http://www.lipsum.com/feed/xml?amount=$amount&what=$what&start=$start")->lipsum;
}

Generating Lorem Ipsum text with PHP, It's basically a bunch of randomly generated Latin words that people use to show what a PHP can be used to generate Lorem Ipsum text . LaTeX has different packages which automatically generates dummy text in our document. You can generate them with just a few lines of code. 1) USING LIPSUM PACKAGE. One of such package is the lipsum package. Lipsum package has access to 150 paragraphs of “Lorem ipsum” dummy text. lipsum[2-4] will print lorem ipsum paragraph 2 to 4.

you have to use it's class , you have to include it in your project with this command echo Lorem::create($lang = Lorem::LANG_FA , $count = 5 , $type = Lorem::TYPE_PARAGRAPH , $end = '...'); then here is the class

class Lorem {
const TYPE_WORD = 1;
const TYPE_CHAR = 2;
const TYPE_PARAGRAPH = 3;
const LANG_EN = 'en';
static private $en = 'Lorem ipsum ';
static function create($lang = self::LANG_FA, $count = 10, $type = self::TYPE_WORD, $end = '...') {
    $out = '';
    if ($lang == self::LANG_FA)
        $lorem = self::$fa;
    else
        $lorem = self::$en;

    switch ($type) {
        case self::TYPE_WORD :
        $words = explode(' ', $lorem);
        $i = 0;
        foreach ($words as $word) {
            if ($i == $count)
                break;
            $out .= ' ' . $word;
            $i++;
        }
        break;
        case self::TYPE_CHAR :
        for ($i = 0; $i <= $count; $i++) {
            $out .= $lorem[$i];
        }
        break;
        case self::TYPE_PARAGRAPH:
        for ($i = 0; $i < $count; $i++) {
            $out .= '<p>' . $lorem . '</p>';
        }
        break;
    }
    return $out . $end;
}

}

Professional lorem ipsum generator for typographers, Lorem Ipsum copy in various charsets and languages for layouts. Most text editors like MS Word or Lotus Notes generate random lorem text when Drupal, Mambo, PHP-Nuke, WordPress, or Movable Type offer Lorem Ipsum plug-ins with  You’ll now see sample, random latin Lorem Ipsum paragraphs in your document, like in the next photo: If you need a specific number of random lorem ipsum text or paragraphs containing a particular number of lines, you can do that quicky by typing =lorem(5,2), where 5 is the number of paragraphs and 2 is the number of sentences of each paragraph.

php: How to get random text from lorem Ipsum in PHP?, I want the random text from Lorem Ipsum so I can use it when generating webpages. I can't find any PHP functions that does this and I'm wondering if there's any  Lorem Ipsum Generator: Creates a button on wysiwyg toolbars to add a configurable amount of Lorem Ipsum text to a post, page or any other custom post type. lorem shortcode : This plugin contains two shortcodes, lorem and loremimage, the loremimage shortcode can be nested in the lorem shortcode.

Lipsum generator: Lorem Ipsum, Various versions have evolved over the years, sometimes by accident, sometimes on Contrary to popular belief, Lorem Ipsum is not simply random text. Contrary to what one might think, the Lorem ipsum text, despite being meaningless, has noble origins. Objectively composed of unrelated words, Lorem ipsum owes its existence to Marco Tullio Cicerone and to some steps of his De finibus bonorum et malorum (The highest good and the highest evil) written in 45 BC , a classic of Latin literature dating back more than 2000 years ago.

Lorem Ipsum, You choose the number of paragraphs, words or lists. You then get a random text that you can then freely use it in your layout. The generated text is the Latin  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

Comments
  • Try this blog.ergatides.com/2011/08/16/…
  • or you can read the three answers that have just copied and pasted this.
  • loripsum.net actually has a much cleaner API that doesn't require XML parsing, and allows better parametrization for filling stock webpages.
  • I've never used file_get_contents() before, the docs say it takes the arguments of a file name, I'm surprised it works with a URL.
  • @Celeritas: The file could be a remote file, too :)
  • Scroll down on that page until you find "A URL can be used as a filename with this function if the fopen wrappers have been enabled. See fopen() for more details on how to specify the filename. See the Supported Protocols and Wrappers for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide." - has been working since PHP 4.2 or so.
  • Celeritas: It depends on 'allow_url_fopen' being enabled in php.ini ;)
  • Much better solution than using (the very excellent) loripsum.net
  • This class is nice solution. Just keep in mind that the dummy text should always start with first sentece "Lorem ipsum dolor sit amet, consectetur adipiscing elit." followed with random words.