Veronica's String form
 
<?php
include('includes/header.php');
// set error reporting to all
error_reporting(E_ALL);
echo('<h1>Veronica\'s String form</h1>');
// set variables
$self = basename($_SERVER['SCRIPT_NAME']);
$step = isset($_POST['step']) ? $_POST['step'] : 0;
$message = isset($_POST['message']) ? $_POST['message'] : '';
// filter all input for browser output
$message = htmlentities($message);
// first time into this form
if ($step == 0)
{
?>
<?php echo($message);?>
<form action="<?php echo($self);?>" method="POST" >
<table>
<tr>
<td class="right">
Write Here:
</td>
<td class="left">
<textarea cols=80 rows=6 name="message">
<?php echo($message);?>
</textarea>
</td>
</tr>
</table>
<p>
<input type="hidden" name="step" value="1">
<input type="submit" name="submit" value="Submit">
</p>
</form>
<?php
} else // perform string magic!
{
$size = strlen($message); // find the length of the string
echo("The length of your string is: $size");
echo('<br><br>');
$user_words = array(); // create an array of words
$user_words = explode(' ', $message); // explode the string into an array
$mixed_string = '';
$backward_string = '';
echo("Here is a breakdown of your string: ");
echo('<br><br>');
$i = 0;
foreach($user_words as $word)
{
// display word number in string
$i += 1;
echo("$word - is word #$i");
echo('<br><br>');
// create mixed up version of each word
$k = strlen($word);
if ($k > 2) {
// get random index number
$r = rand(0,($k - 1));
// loop through letters in word
for ($j = 0; $j < $k; $j++)
{
// subtract 1 from random index number to move left over letters
$mixed_string = $mixed_string . substr($word, ($r - $j), 1);
}
} else {
$mixed_string = $mixed_string . $word;
}
$mixed_string = $mixed_string . ' ';
}
// create backward version of string
for ($m = ($size - 1); $m >= 0; $m--) {
// build backward message
$backward_string = $backward_string . $message[$m];
}
?>
<!-- show results & provide a button to try again -->
<h3>
<?php echo("After mixing up your string, you get: "); ?>
</h3>
<h4>
<?php echo($mixed_string); ?>
</h4>
<br><br>
<h3>
<?php echo("Your string backwards is: "); ?>
</h3>
<h4>
<?php echo($backward_string); ?>
</h4>
<br><br>
<form action="<?php echo($self);?>" method="POST" >
<input type="hidden" name="step" value="0">
<input type="submit" name="submit" value="Try Again">
</form>
<?php
} // end of else block
include('includes/footer.php');
?>