How to get random text from lorem Ipsum in PHP?
$content = file_get_contents('http://loripsum.net/api');
Docs in the bottom right of this page:
Just do a GET request on loripsum.net/api, to get some placeholder text. You can add extra parameters to specify the output you're going to get. Say, you need 10 short paragraphs with headings, use http://loripsum.net/api/10/short/headers. All of the possible parameters are:
- (integer) - The number of paragraphs to generate.
- short, medium, long, verylong - The average length of a paragraph.
- decorate - Add bold, italic and marked text.
- link - Add links.
- ul - Add unordered lists.
- ol - Add numbered lists.
- dl - Add description lists.
- bq - Add blockquotes.
- code - Add code samples.
- headers - Add headers.
- allcaps - Use ALL CAPS.
- prude - Prude version.
- plaintext - Return plain text, no HTML.
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.