Last active
October 27, 2022 15:51
-
-
Save brandonbarringer/803502727240a5fa4f142771c0be3a36 to your computer and use it in GitHub Desktop.
Generate Random Lorem Ipsum in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
abstract class Text { | |
public static function characters($nCharacters = 1) { | |
$words = self::$words; | |
$characters = ''; | |
while (strlen($characters) < $nCharacters) { | |
$characters .= self::words() . ' '; | |
} | |
$characters = substr($characters, 0, $nCharacters); | |
return $characters; | |
} | |
public static function words($nWords = 1, $comma = false) { | |
$words = self::$words; | |
$output = ''; | |
for ($i = 0; $i < $nWords; $i++) { | |
$separator = $comma ? self::wordSeparator() : ' '; | |
$output .= $words[array_rand($words)] . $separator; | |
} | |
return ucfirst(rtrim($output, ', ')); | |
} | |
public static function sentences($nSentences = 1) { | |
$sentences = []; | |
$words = rand(3, 15); | |
for ($ns = 0; $ns < $nSentences; $ns++) { | |
$sentences[] = ucfirst(trim(self::words($words, true))) . self::endPunctuation(); | |
} | |
return implode(' ', $sentences); | |
} | |
public static function paragraphs($nParagraphs = 1) { | |
$paragraphs = []; | |
$sentences = rand(3, 10); | |
for ($np = 0; $np < $nParagraphs; $np++) { | |
$paragraphs[] = '<p>' . trim(self::sentences($sentences)) . '</p>'; | |
} | |
return implode(' ', $paragraphs); | |
} | |
private static function weightedRandom($array) { | |
$total = 0; | |
foreach ($array as $item) { | |
$total += $item['weight']; | |
} | |
$random = (float)mt_rand() / (float)mt_getrandmax() * $total; | |
$current = 0; | |
foreach ($array as $item) { | |
$current += $item['weight']; | |
if ($random <= $current) { | |
return $item['value']; | |
} | |
} | |
} | |
private static function endPunctuation() { | |
$punctuation = [ | |
'period' => [ | |
'weight' => 0.8, | |
'value' => '.' | |
], | |
'question' => [ | |
'weight' => 0.1, | |
'value' => '?' | |
], | |
'exclamation' => [ | |
'weight' => 0.1, | |
'value' => '!' | |
] | |
]; | |
return self::weightedRandom($punctuation); | |
} | |
private static function wordSeparator() { | |
$punctuation = [ | |
'comma' => [ | |
'weight' => 0.1, | |
'value' => ', ' | |
], | |
'space' => [ | |
'weight' => 0.9, | |
'value' => ' ' | |
] | |
]; | |
return self::weightedRandom($punctuation); | |
} | |
private static $words = ['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', 'i']; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment