PHP Word Scrambler - String Questions

masterjake

New Member
Messages
73
Reaction score
0
Points
0
Hey guys,

I'm making a php word scrambler and heres how it works.

A person types all the words they want to scramble into a large textarea text field, separating each word by a comma, and then they press scramble. The php code splits each group of words between a set of commas into a seperate string and then scrambles the new word then converts the comma to a line break "<br>" so that when I display the results it seperates each scrambled word by a line. The only problem is I'm very lost as to how to do this. I know I will need the str_shuffle() function but can someone point out how to do the rest.
 

Scoochi2

New Member
Messages
185
Reaction score
0
Points
0
assuming your form looks vaguely like the following:
HTML:
<form action='scramble.php' method='post'>
<textarea name='words'></textarea>
<input type='submit' value='Scramble!'>
</form>
then the following should work (I've included comments so you see what I'm doing and why, and hopefully you'll understand how it works :)):
scramble.php
PHP:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
  <head>
  <title>Word Scrambler Results</title>
  <meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
</head>
<body>
<?php
if (isset($_POST['words']))
  $words_s = $_POST['words'];
else
  die ('Give me a list of words, separated by a comma. <a href="http://forums.x10hosting.com/programming-help/index.html">Go back</a></body></html>');

$words_s = trim($words_s);
// we don't want spaces or anything like that in the words, so we use trim to get rid of whitespace.
$words_a = explode(',',$words_s);
// explode will convert a string into an array, with the first parameter being the delimiter.

echo "<p>\n";
foreach ($words_a as $word) // foreach will iterate through the array, with each value being assigned to the variable $word.
  { // this is what we do for each array value.
  $word = str_shuffle($word); // str_shuffle simply reorders the characters in a string.
  echo "$word<br>\n";
  }

?><br>
There are no more words left to scramble!</p>
</body></html>
 
Last edited:
Top