Login   Register  
PHP Classes
elePHPant
Icontem

File: class.randomWord.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of kumar mcmillan  >  randomWord  >  class.randomWord.php  >  Download  
File: class.randomWord.php
Role: ???
Content type: text/plain
Description: class
Class: randomWord
makes a random 'readable' word (example: Huchafo!)
Author: By
Last change:
Date: 12 years ago
Size: 2,953 bytes
 

Contents

Class file image Download
<?php

/*!

class: randomWord
released: 2002-04-27
author: kumar mcmillan (kumar@chicagomodular.com)
class description: 

makes a random 'readable' word (currently english-only). this is useful if you want to randomly generate passwords that your user can read and remember (instead of giving them a bunch of numbers).  some examples: Huchafo, Pequizu, Zequiju, Chomoxy ... ).
have fun.

license: The GNU General Public License (GPL)
http://www.opensource.org/licenses/gpl-license.html

requirements: PHP >= 4.0

!*/

class randomWord
{	
	// formatting preferences 
	// (decisions are made in the order your see here):
	var $makeMoreRandom = TRUE; // it takes a little longer
	var $isLowerCase = TRUE;
	var $isUCFirst = TRUE; // first letter upper-case, the rest lower
	var $isUpperCase = FALSE;
	var $shoutIt = FALSE; // example: chivoy!
	
	// private:
	var $vowels = array("a","e","i","o","u","y");
	var $consonants = array(array()); // see the constructor
	var $word = "";
	
	// constructor:
	function randomWord()
	{
		$this->consonants[0] = array(
		"b","c","d","f","g","h","j","k","l","m","n","p","r","s","t","v","w","z");
		$this->consonants[1] = array("ch","qu","th","xy");
	}
	
	// ------------------------------------------------------- |
	
	// public:
	function set($var,$value)
	{
		$this->$var = $value;
	}
	
	// public:
	function buildWord($length=10)
	{
		$done = FALSE;
		$cons_or_vowel = 1;
		
		// makes the word:
		while(!$done){
			$this->seed();
			// 1 adds a consonant:
			if(1==$cons_or_vowel){
				$i = rand(0,1);
				$add = $this->consonants[$i][array_rand($this->consonants[$i])];
				$cons_or_vowel = 2;
			}
			// 2 adds a vowell:
			elseif(2==$cons_or_vowel){
				$add = $this->vowels[array_rand($this->vowels)];
				$cons_or_vowel = 1;
			}
			$this->word .= $add;
			if(strlen($this->word)>=$length) $done=TRUE;
		}
		// truncate word to fit desired length: 
		// (in case a double-consonant was added for the last char build)
		$this->word = substr($this->word,0,$length);
		// change case according to var prefs:
		$this->formatCase();
		// shout it:
		if($this->shoutIt) $this->word .= "!";
		
		return $this->word;
	}
	
	// public: 
	// (you need to call buildWord() first though)
	function addNumbers($length=4)
	{
		for($i=1; $i<=$length; $i++){
			$this->seed();
			$this->word .= (string) rand(0,9);
		}
		return $this->word;
	}
	
	// ------------------------------------------------------- |
	
	// private:
	function formatCase()
	{
		if($this->isLowerCase) $this->word = strtolower($this->word);
		if($this->isUCFirst) $this->word = ucfirst(strtolower($this->word));
		if($this->isUpperCase) $this->word = strtoupper($this->word);
		
		return $this->word;
	}
	
	// private:
	function seed()
	{
		if($this->makeMoreRandom) usleep(1);
		srand((double)microtime()*1000000);
	}
	
} // end of class
?>