You are here

public static function SampleDataLoader::getLipsum in YAML Content 8.2

Helper function to generate random Lorem Ipsum content.

Parameters

int $length: The integer length of the content to generate.

bool $capitalize: Whether to capitalize the return Lorem ipsum.

Return value

string The string of content, $length characters long.

1 call to SampleDataLoader::getLipsum()
SampleDataLoader::loadSample in modules/sample_data/src/SampleDataLoader.php
Load sample data based on type and additional parameters.

File

modules/sample_data/src/SampleDataLoader.php, line 206

Class

SampleDataLoader
Provides methods for retrieving sample data to be used in demo content.

Namespace

Drupal\sample_data

Code

public static function getLipsum($length = 200, $capitalize = TRUE) {
  $lorem_ipsum = file_get_contents(__DIR__ . '/lipsum.txt');
  $lipsum_count = strlen($lorem_ipsum);
  $rand_start = max(0, random_int(0, $lipsum_count - $length));
  $start = $rand_start ? strpos($lorem_ipsum, ' ', $rand_start) + 1 : 0;
  $lipsum = preg_replace('/^[\\W_]+|[\\W_]+$/', '', substr($lorem_ipsum, $start, $length));
  $lipsum = $capitalize ? ucfirst($lipsum) : $lipsum;
  $missing_char = $length - strlen($lipsum);
  return $missing_char ? $lipsum . SampleDataLoader::getLipsum($missing_char, FALSE) : $lipsum;
}