<?php
/*
 * ========================================================================================
 * File: Latinizer.php
 * Function: Take a string input and parse the word(s) into their respective pig-latin form
 * Author: Coolhand2 (Michael Sherwood)
 * Email: coolhand2@gmail.com
 * Copyright (c) 2006 by Coolhand2
 * Lines of actual code: 35
 * ========================================================================================
 */
?>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="GET">
<input type="box" name="phrase" />
<input type="submit" value="Translate!" />
</form> 
<?php
$vowelArray = array('a', 'e', 'i', 'o', 'u'); //Vowels to look for.
 
//We don't wanna include these in the latinizing process, so we take these out...
$delimiterArray = array('.' , ',' , '!' , ':' , ';' , '?', '~'); 
 
$endString = "ay"; //If it is latinized, we staple this on the end.
 
$phrase = $_GET['phrase'];
if( $phrase != '' )
{
	/*
	 * ===========================================================
	 * Get rid of any excess white space included in the phrase
	 * Sperate the words into an array that we can iterate through
	 * Iterate through each word.
	 * ===========================================================
	 */
	$phrase = trim( $phrase );
	$phraseArray = explode( " ", $phrase ); //Seperate the words through spaces.
	foreach( $phraseArray as $word ) 
	{
		/*
		 * =================================================
		 * Iterate through each word
		 * So as to not get a result of "oot!shay" 
		 * get the last known position of a puncuation mark.
		 * =================================================
		 */
		$puncPos = strlen( $word );
		while( in_array( $word{$puncPos}, $delimiterArray ) ) { $puncPos--; }
 
		/*
		 * ========================================================================================
		 * Now we go through the word itself, and see if we can't find the first vowel in the word.
		 * We use $i to keep track of the location of the mythical first vowel in the word
		 * ========================================================================================
		 */
		$i = 0; //Set $i to the first character position in any word.
		while( !in_array( $word{$i}, $vowelArray ) ) { $i++; } 
 
		if( $i != $puncPos )
		{
			/*
			 * ================================================================
			 * Concatenate the string parts together
			 * 	Segment of the word from the first vowel to the punctuation
			 * 	Segment of the word from the beginning of the word to $i
			 * 	"ay"
			 * 	Then any punction there may be.
			 * ================================================================
			 */
			$string = "";
			$string .= substr( $word, $i, ($puncPos - $i ) - 1);
			$string .= substr( $word, 0, $i );
			$string .= $endString;
			$string .= substr( $word, $puncPos - 1 );
 
			$newPhrase[] = $string;
		} else { $newPhrase[] = $word; }
	}
 
	/*
	 * =======================================================
	 * When that's all said and done for each word, 
	 * we then put all the words back the way they used to be!
	 * =======================================================
	 */
 
	$string = implode( " ", $newPhrase );
	echo $string;
 
}
else { echo "Input a string!"; }
?>