Location: PHPKode > scripts > Kenny Translator > kenny-translator/Kenny.class.php
<?php
/**
 * @author Nemanja Avramovic
 * @version 0.1
 * @category SouthPark
 * 
 * KennyTranslator -- a kennyfier <br>
 * Copyright 2002 Kohan Ikin <hide@address.com> <br>
 * Ported to PHP by Nemanja Avramovic, avramyu AT gmail DOT com<br>
 * <br>
 * (thanks to Sebastian Rühl for JAVA version, it helped me alot)<br>
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 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 Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * 
 */

class Kenny {
	
	/**
	* The KENNYLETTERS in alphabetical order. Big Letters are the Same with the
	* only difference That the First char is UpperCase
	* 
	*/
	
	var $kennyletters = array("mmm", "mmp", "mmf", "mpm",
			"mpp", "mpf", "mfm", "mfp", "mff", "pmm", "pmp", "pmf", "ppm",
			"ppp", "ppf", "pfm", "pfp", "pff", "fmm", "fmp", "fmf", "fpm",
			"fpp", "fpf", "ffm", "ffp");
	
	/**
	 * Used to store old error_reporting level, default: E_ALL
	 *
	 */
	var $errrep = E_ALL;
	
		
	/**
	 * Translates single Kenny letter to single latin letter.
	 *
	 * @param string $s
	 * @return string[0]
	 */
			
	function _translateKennyLetterToNormalLetter($s) {
		$bigchar = ($s[0] == strtoupper($s[0])) ? true : false;
		$s[0] = strtolower($s[0]);
		
		for($i=0; $i<count($this->kennyletters);$i++) {
			if ($s == $this->kennyletters[$i]) {
				return $bigchar ? chr($i + ord('A')) : chr($i + ord('a'));
			}
		}
		
	}
	
	/**
	 * Translates whole kennyfied string to normal string.
	 *
	 * @param string $s
	 * @return string
	 */
	
	function _translateKennyToNormal($s) {
		$result = "";
		
		for ($i=0; $i<strlen($s); $i+=3) {
			
			while (strtoupper($s[$i]) != 'M' && strtoupper($s[$i]) != 'P' && strtoupper($s[$i]) != 'F') {
				$result .= $s[$i];
				$i++;
				if ($i == strlen($s)) break;
			}
			
			$word = substr($s,$i,$i+3);
			$result .= $this->_translateKennyLetterToNormalLetter($word[0].$word[1].$word[2]);
			
		}
		
		return $result;
	}
	
	/**
	 * Translates single latin letter (char) to kennyfied letter
	 *
	 * @param char $c
	 * @return string
	 */
	
	function _translateNormalLetterToKennyLetter($c) {
		if ($c == strtoupper($c)) {
			$result = $this->kennyletters[ord($c) - ord('A')];
			$z = $result[0];
			$result[0] = strtoupper($z);
			return $result;
		}
		else {
			return $this->kennyletters[ord($c) - ord('a')];
		}
	}
	
	/**
	 * Translates whole string to kennyfied string
	 *
	 * @param string $s
	 * @return string
	 */
	
	function _translateNormalToKenny($s) {
		$result = "";
		
		for ($i=0; $i<strlen($s); $i++) {
			$c = $s[$i];
			
			if ((ord($c) < ord('A') || ord($c) > ord('Z')) && (ord($c) < ord('a') || ord($c) > ord('z'))) {
				$result .= $c;
			}
			else {
				$result .= $this->_translateNormalLetterToKennyLetter($c);
			}
			
		}
		
		return $result;
	}
	
	/**
	 * Checks whether given string is already kennyfied or not?
	 *
	 * @param string $s
	 * @return boolean
	 */
	
	function _isKenny($s) {
		for ($i=0; $i<strlen($s); $i++) {
			$c = ord($s[$i]);
			if ( ($c>=ord('a') && $c<=ord('z') && $c!=ord('m') && $c!=ord('f') && $c!=ord('p'))
						|| ($c>=ord('A') && $c<=ord('Z') && $c!=ord('M') && $c!=ord('F') && $c!=ord('P')) ) {
				return false;
			}
		}
		return true;
	}
	
	/**
	 * Main function of class. Translates given string to or from kennyfied string (it'll
	 *  autodetect whether given string is already kennyfied or not).
	 *
	 * @param string $string
	 * @return string
	 */
	
	function translate($string) {
		return (!$this->_isKenny($string)) ? $this->_translateNormalToKenny($string) : $this->_translateKennyToNormal($string);
	}
	
	/**
	 * Constructor for class. It will get current error_reporting level and place it in
	 * $this->errrep variable, then it'll turn off notices and warnings for error_reporting
	 * because there are some notices while converting from Kenny speech to normal.
	 * 
	 * You can specify whether to turn off notices and warnings as parameter. The default
	 * value is TRUE, and it is recommended value. Use FALSE only if you're debugging application.
	 *
	 * @param boolean $disable_error_reporting
	 * @return Kenny
	 */
	
	function Kenny($disable_error_reporting = true) {
		if ($disable_error_reporting) {
			$this->errrep = @error_reporting(E_ERROR);
		}
		else {
			$this->errrep = @error_reporting();
		}
	}
	
	/**
	 * Improvised destructor for class. It'll restore old error_reporting level. Not required to be used,
	 * but it has great name, and it's used like this: $Kenny->kill(); 
	 * :D  
	 *
	 */
	
	function kill() {
		@error_reporting($this->errrep);
		
		// You bastards! ;)
	}
	
}


?>
Return current item: Kenny Translator