Skip to content

Lorem Ipsum Generator — PHP source

Generate placeholder Lorem Ipsum text - paragraphs and sentences with adjustable counts. Classic Latin word pool, instant copy.

This is the PHP implementation — the same logic the interactive tool runs, in a shareable, citable form.

<?php
/**
 * Lorem Ipsum generator — PHP polyglot showcase port.
 *
 * Pure structure generator: random word selection from the canonical Lorem
 * Ipsum pool, assembled into capitalized sentences, paragraphs, and
 * multi-paragraph blocks. Mirrors the TypeScript reference implementation
 * in src/lib/lorem.ts 1:1 (same word pool, same defaults, same edge-case
 * handling, same non-crypto randomness).
 *
 * Exposed as a static class: `Lorem::sentence()`, `Lorem::paragraph(4)`,
 * `Lorem::lorem(3, 5)`, `Lorem::words()`. PHP has native default parameters,
 * so the default values match the TypeScript source exactly.
 *
 * Ported from src/lib/lorem.ts.
 * Display source — part of CosmoDev's polyglot tool pages.
 */

declare(strict_types=1);

namespace CosmoDev;

/**
 * Static Lorem Ipsum generator. Encapsulates the word pool and the private
 * RNG helper alongside the three public structure builders.
 */
final class Lorem
{
    /** @var array<int,string>|null Lazily-initialized word pool. */
    private static ?array $words = null;

    /**
     * The canonical Lorem Ipsum word pool, split from the same string as the
     * TypeScript source. Computed once and cached — the PHP analogue of the
     * TS module-level `WORDS` constant.
     *
     * @return list<string>
     */
    public static function words(): array
    {
        if (self::$words === null) {
            self::$words = explode(' ', implode(' ', [
                'lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor',
                'incididunt ut labore et dolore magna aliqua enim ad minim veniam quis nostrud exercitation',
                'ullamco laboris nisi aliquip ex ea commodo consequat duis aute irure in reprehenderit voluptate',
                'velit esse cillum eu fugiat nulla pariatur excepteur sint occaecat cupidatat non proident sunt',
                'culpa qui officia deserunt mollit anim id est laborum',
            ]));
        }
        return self::$words;
    }

    /**
     * Uniform integer in [0, $max). Uses mt_rand() (Mersenne Twister), PHP's
     * general-purpose PRNG — a deliberate match for the TypeScript source's
     * non-crypto Math.random(). Not for security-sensitive use. A non-positive
     * bound yields 0 so mt_rand never receives a negative upper limit (which
     * throws under PHP 8).
     */
    private static function randInt(int $max): int
    {
        if ($max <= 0) {
            return 0;
        }
        return mt_rand(0, $max - 1);
    }

    /**
     * Build a single sentence of between $minWords and $maxWords words
     * (inclusive). When $maxWords < $minWords the range collapses to exactly
     * $minWords words via the max(1, ...) span guard.
     */
    public static function sentence(int $minWords = 8, int $maxWords = 16): string
    {
        $span = max(1, $maxWords - $minWords + 1);
        $n = $minWords + self::randInt($span);

        $pool = self::words();
        $poolSize = count($pool);
        $words = [];
        for ($i = 0; $i < $n; $i++) {
            $words[] = $pool[self::randInt($poolSize)];
        }

        $body = implode(' ', $words);
        // ucfirst() uppercases only the first character and leaves the rest
        // untouched — equivalent to charAt(0).toUpperCase() + slice(1).
        return ucfirst($body) . '.';
    }

    /** Join $sentences default-length (8..16 word) sentences into a paragraph. */
    public static function paragraph(int $sentences = 5): string
    {
        $parts = [];
        for ($i = 0; $i < $sentences; $i++) {
            $parts[] = self::sentence();
        }
        return implode(' ', $parts);
    }

    /** Build $paragraphs paragraphs of $sentencesPerPara sentences each. */
    public static function lorem(int $paragraphs = 3, int $sentencesPerPara = 5): string
    {
        $parts = [];
        for ($i = 0; $i < $paragraphs; $i++) {
            $parts[] = self::paragraph($sentencesPerPara);
        }
        return implode("\n\n", $parts);
    }
}

Also available in 13 other languages

Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →