Lore ipsum generator for PHP

Everything todo with programming goes HERE.
Post Reply
nux
Round Winner
Posts: 206
Joined: Mon Sep 12, 2011 11:20 pm

Lore ipsum generator for PHP

Post by nux »

Somebody might find this useful, i do have some more features in mind, but this will do for now.

Code: Select all

<?php
/*
	Author: Matias Pino
	Year: 2012
	Version: 1.0-alpha
	
	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

class lorsum {

	private $words = array('lorem','ipsum','dolor','sit','amet','consectetur','adipiscing','elit','curabitur','vel','hendrerit','libero','eleifend','blandit','nunc','ornare','odio','ut','orci','gravida','imperdiet','nullam','purus','lacinia','a','pretium','quis','congue','praesent','sagittis','laoreet','auctor','mauris','non','velit','eros','dictum','proin','accumsan','sapien','nec','massa','volutpat','venenatis','sed','eu','molestie','lacus','quisque','porttitor','ligula','dui','mollis','tempus','at','magna','vestibulum','turpis','ac','diam','tincidunt','id','condimentum','enim','sodales','in','hac','habitasse','platea','dictumst','aenean','neque','fusce','augue','leo','eget','semper','mattis','tortor','scelerisque','nulla','interdum','tellus','malesuada','rhoncus','porta','sem','aliquet','et','nam','suspendisse','potenti','vivamus','luctus','fringilla','erat','donec','justo','vehicula','ultricies','varius','ante','primis','faucibus','ultrices','posuere','cubilia','curae','etiam','cursus','aliquam','quam','dapibus','nisl','feugiat','egestas','class','aptent','taciti','sociosqu','ad','litora','torquent','per','conubia','nostra','inceptos','himenaeos','phasellus','nibh','pulvinar','vitae','urna','iaculis','lobortis','nisi','viverra','arcu','morbi','pellentesque','metus','commodo','ut','facilisis','felis','tristique','ullamcorper','placerat','aenean','convallis','sollicitudin','integer','rutrum','duis','est','etiam','bibendum','donec','pharetra','vulputate','maecenas','mi','fermentum','consequat','suscipit','aliquam','habitant','senectus','netus','fames','quisque','euismod','curabitur','lectus','elementum','tempor','risus','cras');
	private $out = '';
	
	function __construct($totalWords) {

		while ($totalWords--) {
			
			// Get a random index to know which word to use
			$index = round(rand(0,count($this->words) - 1));
			$this->out .= ' ';
			if ($this->out === ' ' || substr($this->out,-2,1) == '.')
				// Last character was a period; we capitalize
				$this->out .= ucfirst($this->words[$index]);
			else
				$this->out .= $this->words[$index];
			
			// We need punctuation, so we use a random number and probability
			$rand = round(rand(0,100));
			if ($rand < 10 || $totalWords === 1)
				$this->out .= '.';
			elseif ($rand < 30)
				$this->out .= ',';
		}
		echo $this->out;
	}
}
Usage examples:

Code: Select all

<p><?php new lorsum(70); ?></p>
<img src="image.png" alt="<?php new lorsum(2) ?>">
There's a difference between knowing your shit, and knowing you're shit. Grammar does matter.
User avatar
sinewav
Graphic Artist
Posts: 6413
Joined: Wed Jan 23, 2008 3:37 am
Contact:

Re: Lore ipsum generator for PHP

Post by sinewav »

Very nice. I do have need of something like this about once a year or so. Gold star for you. :star:
epsy
Adjust Outside Corner Grinder
Posts: 2003
Joined: Tue Nov 07, 2006 6:02 pm
Location: paris
Contact:

Re: Lore ipsum generator for PHP

Post by epsy »

Why is this a class instead of a function? It doesn't even have one method!
nux
Round Winner
Posts: 206
Joined: Mon Sep 12, 2011 11:20 pm

Re: Lore ipsum generator for PHP

Post by nux »

epsy wrote:Why is this a class instead of a function? It doesn't even have one method!
It will have methods, i think, im not sure. I took it from another code, i was going to replace only a few things, but ended up writing the whole thing with the exception of the words list. Nothing is said yet. Also, didnt know it would break the layout.
There's a difference between knowing your shit, and knowing you're shit. Grammar does matter.
User avatar
kyle
Reverse Outside Corner Grinder
Posts: 1876
Joined: Thu Jun 08, 2006 3:33 pm
Location: Indiana, USA, Earth, Milky Way Galaxy, Universe, Multiverse
Contact:

Re: Lore ipsum generator for PHP

Post by kyle »

Yay for infinate length code boxes
Image
User avatar
sinewav
Graphic Artist
Posts: 6413
Joined: Wed Jan 23, 2008 3:37 am
Contact:

Re: Lore ipsum generator for PHP

Post by sinewav »

kyle wrote:Yay for infinate length code boxes
Yeah, I was going to say something about that. Would be nice to get that down to 80 chrs wide.
Post Reply