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.

[php]

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

English:

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



<< Back background

[/php]

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

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    
<html>
<head>
	<title>Pig Latin Translator</title>
	<link rel="stylesheet" type="text/css" href="styles_3.css">
</head>

<body>

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

<br>
<center>
<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-->
<br><br>
<h4>Display English with translation?
<br><br>
<input type="radio" name="answer" value="yes">Yes
&nbsp;&nbsp;&nbsp;
<input type="radio" name="answer" value="no">No
</h4>
<!--Submit button-->
<br>
<input type="submit" value="Submit">
</form>
</center>


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

</html>
Sponsor our Newsletter | Privacy Policy | Terms of Service