Pig Latin Translator - trouble when words start with one consonant

I’m trying to make a pig latin translator, but when I input a word like “for” it would output “orfay foay”. “orfay” is correct but I don’t know where “foay” is coming from.


Pig Latin Translator

Pig Latin Translator

<?php $text = $_POST['text']; if ($text == '') print("Nothing was entered."); //if textarea is blank else $words = explode(" ", $text); //put words into an array

foreach ($words as $aWord){
$firstLetter = substr($aWord, 0, 1); //take the first letter
//$restOfTheWord = substr($aWord, 1, strlen($aWord) -1); //take the rest of the word

if (strstr("aeiouAEIOU", $firstLetter)) { 
$newWord = $aWord . "way"; //if  first letter is a vowel ex. is = isway
print $newWord . " ";
} else { //if first letter is a consonant
$secondLetter = substr($aWord, 1, 1); //take the second letter

if (strstr("aeiousAEIOU", $secondLetter)) {
$restOfTheWord = substr($aWord, 1, strlen($aWord) -1); //if second letter is a vowel
$newWord = $restOfTheWord . $firstLetter . 'ay'; //ex. for = orfay
print $newWord . " ";
} else {
$thirdLetter = substr($aWord, 2, 1); //take the third letter

if (strstr("aeiousAEIOU", $thirdLetter)) {
$restOfTheWord = substr($aWord, 2, strlen($aWord) -1); //if third letter is a vowel
$newWord = $restOfTheWord . $firstLetter . $secondLetter . 'ay'; //ex. character = aracterchay
print $newWord . " ";
} else {
$fourthLetter = substr($aWord, 3, 1); //take the fourth letter
$restOfTheWord = substr($aWord, 3, strlen($aWord) -1);
$newWord = $restOfTheWord . $firstLetter . $secondLetter . $thirdLetter . 'ay';
print $newWord;

}//end else

}//end foreach


<?php if(isset($_POST['answer']) && $_POST['answer'] == 'yes') echo htmlspecialchars($_POST['text']); //if selected YES print English ?>

<< Back background


Here’s the html for the page too (if needed)

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
	<title>Pig Latin Translator</title>
	<link rel="stylesheet" type="text/css" href="styles_3.css">


<div id=header>
<h1>Pig Latin Translator</h1>
<h3>Translate English into Pig Latin!
There should be no punctuation except for the final ("."). </h3>

<form action="http://i6.cims.nyu.edu/~sll345/assign4.php" method="post">
<!--text box-->
<textarea name="text" cols="40" rows="7">Enter characters for translation</textarea>
<!--Yes/No Option-->
<h4>Display English with translation?
<input type="radio" name="answer" value="yes">Yes
<input type="radio" name="answer" value="no">No
<!--Submit button-->
<input type="submit" value="Submit">

<img src="pig.jpg" alt="background" id="background">

Sponsor our Newsletter | Privacy Policy | Terms of Service