PHP Lorem Ipsum

The other day I needed to populate a database with some placeholder content. Doing this manually was out of the question so I decided I’d find a text generator, specifically a Lorem Ipsum generator. For anyone unaware, Lorem Ipsum is non-sense, placeholder text used in publishing and design. It allows the developer to see their work completely populated with text, without having to actually create the text. Obviously, for this purpose, any kind of text generator would work to some extent, but traditionally Lorem Ipsum is used.

To get to the point, I successfully located several web-based generators, but no stand-alone PHP class or function. To be honest, I didn’t look too hard and someone a little more determined not to write any code most likely would have found it, but I decided to create my own PHP Lorem Ipsum generator. Here is a rundown of some of the features in the current version:

PHP Lorem Ipsum Generator
Version: 1.0
License: BSD

Download - Version 1.0

Features

  • Generates content in three modes: Plain, HTML (content blocks nested in <p> tags), and Text (plain text in paragraph form)
  • Sentences are punctuated and vary in length based on statistics collected here: http://hearle.nahoo.net/Academic/Maths/Sentence.html. Sentence length will vary on a Guassian distribution.
  • HTML output is ‘clean-code’ formatted with tabs and new lines rather than just blobs of code
  • More output formats to come…

Feel free to request additional features.

Usage

The only public method in the class is getContent.

Description
string getContent( int $wordCount [, string $format = html] [, boolean $loremipsum = true] )

Returns the desired amount of content as a string.

Parameters

wordCount
The number of words to be returned.

format
The output mode, one of ‘html’, ‘txt’, or ‘plain’. HTML by default.

  • HTML: The content is divided into paragraphs, using the paragraph ( <p></p> ) tag.
  • Text: The content is divided into paragraphs with the leading line of each paragraph tabbed
  • Plain: The content is returned unformatted

loremipsum
Whether or not the content should begin with ‘Lorem ipsum’. True by default.

Example

require('LoremIpsum.class.php');
$generator = new LoremIpsumGenerator;
//100 words in html format
$generator->getContent(100);
//100 words without any formatting
$generator->getContent(100, 'plain');
//100 words with 'text' formatting
$generator->getContent(100, 'txt');
//100 words with html format, not beginning with lorem ipsum
$generator->getContent(100, NULL, false);
//or
$generator->getContent(100, 'html', false);

Additional Notes
Both the HTML and Text output modes use paragraph formatting. The mean word count of each paragraph is predetermined and can be set in the constructor, currently the default is 100. Note that this is the mean word count, the actual word count for each paragraph will vary in the same way the length of each sentence will vary.

Changelog
Version 1.1 (Planned)

  • Additional output modes. List mode and possibly more

Version 1.0:

  • Initial Release

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 

9 thoughts on “PHP Lorem Ipsum

  1. Tinley,

    Very nice work. Thanks for putting in the time on this, the generator works perfectly. I am working to add support for multiple languages/dictionaries to use with this.

    Thanks!

  2. Sorry, in my previous post I suggested the punctuate function can do the uppercasing. Stupid me, it could be in getPlain: just using ucfirst on the 0 element from the “s” array inside the foreach loop for the sentences array, does the trick.

    Very nice.

    Regards, MV

    • I actually decided to implement it in the punctuate function.

      private function punctuate(& $sentence)
      {
      	$count = count($sentence);
      	$sentence[$count - 1] = $sentence[$count - 1] . '.';
      	if($count < 4)
      		return $sentence;
      	$commas = $this->numberOfCommas($count);
      	for($i = 1; $i <= $commas; $i++)
      	{
      		$index = (int) round($i * $count / ($commas + 1));
      		if($index < ($count - 1) && $index > 0)
      		{
      			$sentence[$index] = $sentence[$index] . ',';
      		}
      	}
      	$sentence[0] = ucfirst($sentence[0]); //capitalize the first word
      }
      
  3. Hi. Thanks for sharing this. This is very useful, specially to generate dummy content for a database programmatically, or to “loremize” a client’s database (for example, I need the relationships in the data, but not the data itself, so I replace the actual data with lorem text, preserving the relationship of the records from different tables).

    I’ve been using Markov chains with text extracted from Latin books from the Gutenberg Project, which creates some (very realistic) Latin dummy text, but the script is painfully slow and memory hungry. (FTR, I used Erasmus Letters and Cicero Orations as source Latin text).

    Your script is lost faster. Even if I replace the words array with a list of three thousand Latin words, it is still faster. (FTR, the Latin list I used was from http://www.math.ubc.ca/~cass/frivs/latin/latin-dict-full.html).

    For your next version, may I suggest uppercase the first letter after a period? I can do it be searching/replacing the returned string, but I think the punctuate function in you class can take care of that too.

    Again, thanks for sharing.

    Regards, MV

    • A couple people have suggested upper casing the first letter in the past but I never actually got around to implementing it. I have a bit of free time now so I think I’m going to go ahead and implement that as well as add a few things. Thanks for reminding me.

      I would be interested in seeing the code you were using before. I never really intended to generate very realistic results. Just a close enough approximation to use for dummy text.

      Thanks,
      -Matt