1
<?php
2

3
/**
4
 * Lorem Ipsum Generator
5
 *
6
 * PHP version 5.3+
7
 *
8
 * Licensed under The MIT License.
9
 * Redistribution of these files must retain the above copyright notice.
10
 *
11
 * @author    Josh Sherman <hello@joshtronic.com>
12
 * @copyright Copyright 2014, 2015, 2016, 2017, 2018, 2019, 2020 Josh Sherman
13
 * @license   http://www.opensource.org/licenses/mit-license.html
14
 * @link      https://github.com/joshtronic/php-loremipsum
15
 */
16

17
namespace joshtronic;
18

19
class LoremIpsum
20
{
21
    /**
22
     * First
23
     *
24
     * Whether or not we should be starting the string with "Lorem ipsum..."
25
     *
26
     * @access private
27
     * @var    boolean
28
     */
29
    private $first = true;
30

31
    /**
32
     * Words
33
     *
34
     * A lorem ipsum vocabulary of sorts. Not a complete list as I'm unsure if
35
     * a complete list exists and if so, where to get it.
36
     *
37
     * @access private
38
     * @var    array
39
     */
40
    private $words = array(
41
        // Lorem ipsum...
42
        'lorem', 'ipsum', 'dolor', 'sit', 'amet', 'consectetur', 'adipiscing', 'elit',
43

44
        // and the rest of the vocabulary
45
        'a', 'ac', 'accumsan', 'ad', 'aenean', 'aliquam', 'aliquet', 'ante',
46
        'aptent', 'arcu', 'at', 'auctor', 'augue', 'bibendum', 'blandit',
47
        'class', 'commodo', 'condimentum', 'congue', 'consequat', 'conubia',
48
        'convallis', 'cras', 'cubilia', 'curabitur', 'curae', 'cursus',
49
        'dapibus', 'diam', 'dictum', 'dictumst', 'dignissim', 'dis', 'donec',
50
        'dui', 'duis', 'efficitur', 'egestas', 'eget', 'eleifend', 'elementum',
51
        'enim', 'erat', 'eros', 'est', 'et', 'etiam', 'eu', 'euismod', 'ex',
52
        'facilisi', 'facilisis', 'fames', 'faucibus', 'felis', 'fermentum',
53
        'feugiat', 'finibus', 'fringilla', 'fusce', 'gravida', 'habitant',
54
        'habitasse', 'hac', 'hendrerit', 'himenaeos', 'iaculis', 'id',
55
        'imperdiet', 'in', 'inceptos', 'integer', 'interdum', 'justo',
56
        'lacinia', 'lacus', 'laoreet', 'lectus', 'leo', 'libero', 'ligula',
57
        'litora', 'lobortis', 'luctus', 'maecenas', 'magna', 'magnis',
58
        'malesuada', 'massa', 'mattis', 'mauris', 'maximus', 'metus', 'mi',
59
        'molestie', 'mollis', 'montes', 'morbi', 'mus', 'nam', 'nascetur',
60
        'natoque', 'nec', 'neque', 'netus', 'nibh', 'nisi', 'nisl', 'non',
61
        'nostra', 'nulla', 'nullam', 'nunc', 'odio', 'orci', 'ornare',
62
        'parturient', 'pellentesque', 'penatibus', 'per', 'pharetra',
63
        'phasellus', 'placerat', 'platea', 'porta', 'porttitor', 'posuere',
64
        'potenti', 'praesent', 'pretium', 'primis', 'proin', 'pulvinar',
65
        'purus', 'quam', 'quis', 'quisque', 'rhoncus', 'ridiculus', 'risus',
66
        'rutrum', 'sagittis', 'sapien', 'scelerisque', 'sed', 'sem', 'semper',
67
        'senectus', 'sociosqu', 'sodales', 'sollicitudin', 'suscipit',
68
        'suspendisse', 'taciti', 'tellus', 'tempor', 'tempus', 'tincidunt',
69
        'torquent', 'tortor', 'tristique', 'turpis', 'ullamcorper', 'ultrices',
70
        'ultricies', 'urna', 'ut', 'varius', 'vehicula', 'vel', 'velit',
71
        'venenatis', 'vestibulum', 'vitae', 'vivamus', 'viverra', 'volutpat',
72
        'vulputate',
73
    );
74

75
    /**
76
     * Word
77
     *
78
     * Generates a single word of lorem ipsum.
79
     *
80
     * @access public
81
     * @param  mixed  $tags string or array of HTML tags to wrap output with
82
     * @return string generated lorem ipsum word
83
     */
84 1
    public function word($tags = false)
85
    {
86 1
        return $this->words(1, $tags);
87
    }
88

89
    /**
90
     * Words Array
91
     *
92
     * Generates an array of lorem ipsum words.
93
     *
94
     * @access public
95
     * @param  integer $count how many words to generate
96
     * @param  mixed   $tags string or array of HTML tags to wrap output with
97
     * @return array   generated lorem ipsum words
98
     */
99 1
    public function wordsArray($count = 1, $tags = false)
100
    {
101 1
        return $this->words($count, $tags, true);
102
    }
103

104
    /**
105
     * Words
106
     *
107
     * Generates words of lorem ipsum.
108
     *
109
     * @access public
110
     * @param  integer $count how many words to generate
111
     * @param  mixed   $tags string or array of HTML tags to wrap output with
112
     * @param  boolean $array whether an array or a string should be returned
113
     * @return mixed   string or array of generated lorem ipsum words
114
     */
115 1
    public function words($count = 1, $tags = false, $array = false)
116
    {
117 1
        $words      = array();
118 1
        $word_count = 0;
119

120
        // Shuffles and appends the word list to compensate for count
121
        // arguments that exceed the size of our vocabulary list
122 1
        while ($word_count < $count) {
123 1
            $shuffle = true;
124

125 1
            while ($shuffle) {
126 1
                $this->shuffle();
127

128
                // Checks that the last word of the list and the first word of
129
                // the list that's about to be appended are not the same
130 1
                if (!$word_count || $words[$word_count - 1] != $this->words[0]) {
131 1
                    $words      = array_merge($words, $this->words);
132 1
                    $word_count = count($words);
133 1
                    $shuffle    = false;
134
                }
135
            }
136
        }
137

138 1
        $words = array_slice($words, 0, $count);
139

140 1
        return $this->output($words, $tags, $array);
141
    }
142

143
    /**
144
     * Sentence
145
     *
146
     * Generates a full sentence of lorem ipsum.
147
     *
148
     * @access public
149
     * @param  mixed  $tags string or array of HTML tags to wrap output with
150
     * @return string generated lorem ipsum sentence
151
     */
152 1
    public function sentence($tags = false)
153
    {
154 1
        return $this->sentences(1, $tags);
155
    }
156

157
    /**
158
     * Sentences Array
159
     *
160
     * Generates an array of lorem ipsum sentences.
161
     *
162
     * @access public
163
     * @param  integer $count how many sentences to generate
164
     * @param  mixed   $tags string or array of HTML tags to wrap output with
165
     * @return array   generated lorem ipsum sentences
166
     */
167 1
    public function sentencesArray($count = 1, $tags = false)
168
    {
169 1
        return $this->sentences($count, $tags, true);
170
    }
171

172
    /**
173
     * Sentences
174
     *
175
     * Generates sentences of lorem ipsum.
176
     *
177
     * @access public
178
     * @param  integer $count how many sentences to generate
179
     * @param  mixed   $tags string or array of HTML tags to wrap output with
180
     * @param  boolean $array whether an array or a string should be returned
181
     * @return mixed   string or array of generated lorem ipsum sentences
182
     */
183 1
    public function sentences($count = 1, $tags = false, $array = false)
184
    {
185 1
        $sentences = array();
186

187 1
        for ($i = 0; $i < $count; $i++) {
188 1
            $sentences[] = $this->wordsArray($this->gauss(24.46, 5.08));
189
        }
190

191 1
        $this->punctuate($sentences);
192

193 1
        return $this->output($sentences, $tags, $array);
194
    }
195

196
    /**
197
     * Paragraph
198
     *
199
     * Generates a full paragraph of lorem ipsum.
200
     *
201
     * @access public
202
     * @param  mixed  $tags string or array of HTML tags to wrap output with
203
     * @return string generated lorem ipsum paragraph
204
     */
205 1
    public function paragraph($tags = false)
206
    {
207 1
        return $this->paragraphs(1, $tags);
208
    }
209

210
    /**
211
     * Paragraph Array
212
     *
213
     * Generates an array of lorem ipsum paragraphs.
214
     *
215
     * @access public
216
     * @param  integer $count how many paragraphs to generate
217
     * @param  mixed   $tags string or array of HTML tags to wrap output with
218
     * @return array   generated lorem ipsum paragraphs
219
     */
220 1
    public function paragraphsArray($count = 1, $tags = false)
221
    {
222 1
        return $this->paragraphs($count, $tags, true);
223
    }
224

225
    /**
226
     * Paragraphss
227
     *
228
     * Generates paragraphs of lorem ipsum.
229
     *
230
     * @access public
231
     * @param  integer $count how many paragraphs to generate
232
     * @param  mixed   $tags string or array of HTML tags to wrap output with
233
     * @param  boolean $array whether an array or a string should be returned
234
     * @return mixed   string or array of generated lorem ipsum paragraphs
235
     */
236 1
    public function paragraphs($count = 1, $tags = false, $array = false)
237
    {
238 1
        $paragraphs = array();
239

240 1
        for ($i = 0; $i < $count; $i++) {
241 1
            $paragraphs[] = $this->sentences($this->gauss(5.8, 1.93));
242
        }
243

244 1
        return $this->output($paragraphs, $tags, $array, "\n\n");
245
    }
246

247
    /**
248
     * Gaussian Distribution
249
     *
250
     * This is some smart kid stuff. I went ahead and combined the N(0,1) logic
251
     * with the N(m,s) logic into this single function. Used to calculate the
252
     * number of words in a sentence, the number of sentences in a paragraph
253
     * and the distribution of commas in a sentence.
254
     *
255
     * @access private
256
     * @param  double  $mean average value
257
     * @param  double  $std_dev stadnard deviation
258
     * @return double  calculated distribution
259
     */
260 1
    private function gauss($mean, $std_dev)
261
    {
262 1
        $x = mt_rand() / mt_getrandmax();
263 1
        $y = mt_rand() / mt_getrandmax();
264 1
        $z = sqrt(-2 * log($x)) * cos(2 * pi() * $y);
265

266 1
        return $z * $std_dev + $mean;
267
    }
268

269
    /**
270
     * Shuffle
271
     *
272
     * Shuffles the words, forcing "Lorem ipsum..." at the beginning if it is
273
     * the first time we are generating the text.
274
     *
275
     * @access private
276
     */
277 1
    private function shuffle()
278
    {
279 1
        if ($this->first) {
280 1
            $this->first = array_slice($this->words, 0, 8);
281 1
            $this->words = array_slice($this->words, 8);
282

283 1
            shuffle($this->words);
284

285 1
            $this->words = $this->first + $this->words;
286

287 1
            $this->first = false;
288
        } else {
289 1
            shuffle($this->words);
290
        }
291
    }
292

293
    /**
294
     * Punctuate
295
     *
296
     * Applies punctuation to a sentence. This includes a period at the end,
297
     * the injection of commas as well as capitalizing the first letter of the
298
     * first word of the sentence.
299
     *
300
     * @access private
301
     * @param  array   $sentences the sentences we would like to punctuate
302
     */
303 1
    private function punctuate(&$sentences)
304
    {
305 1
        foreach ($sentences as $key => $sentence) {
306 1
            $words = count($sentence);
307

308
            // Only worry about commas on sentences longer than 4 words
309 1
            if ($words > 4) {
310 1
                $mean    = log($words, 6);
311 1
                $std_dev = $mean / 6;
312 1
                $commas  = round($this->gauss($mean, $std_dev));
313

314 1
                for ($i = 1; $i <= $commas; $i++) {
315 1
                    $word = round($i * $words / ($commas + 1));
316

317 1
                    if ($word < ($words - 1) && $word > 0) {
318 1
                        $sentence[$word] .= ',';
319
                    }
320
                }
321
            }
322

323 1
            $sentences[$key] = ucfirst(implode(' ', $sentence) . '.');
324
        }
325
    }
326

327
    /**
328
     * Output
329
     *
330
     * Does the rest of the processing of the strings. This includes wrapping
331
     * the strings in HTML tags, handling transformations with the ability of
332
     * back referencing and determining if the passed array should be converted
333
     * into a string or not.
334
     *
335
     * @access private
336
     * @param  array   $strings an array of generated strings
337
     * @param  mixed   $tags string or array of HTML tags to wrap output with
338
     * @param  boolean $array whether an array or a string should be returned
339
     * @param  string  $delimiter the string to use when calling implode()
340
     * @return mixed   string or array of generated lorem ipsum text
341
     */
342 1
    private function output($strings, $tags, $array, $delimiter = ' ')
343
    {
344 1
        if ($tags) {
345 1
            if (!is_array($tags)) {
346 1
                $tags = array($tags);
347
            } else {
348
                // Flips the array so we can work from the inside out
349 1
                $tags = array_reverse($tags);
350
            }
351

352 1
            foreach ($strings as $key => $string) {
353 1
                foreach ($tags as $tag) {
354
                    // Detects / applies back reference
355 1
                    if ($tag[0] == '<') {
356 1
                        $string = str_replace('$1', $string, $tag);
357
                    } else {
358 1
                        $string = sprintf('<%1$s>%2$s</%1$s>', $tag, $string);
359
                    }
360

361 1
                    $strings[$key] = $string;
362
                }
363
            }
364
        }
365

366 1
        if (!$array) {
367 1
            $strings = implode($delimiter, $strings);
368
        }
369

370 1
        return $strings;
371
    }
372
}
373

Read our documentation on viewing source code .

Loading