If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

String manipulation

I really hope someone can point me in the right direction. I am in the 5th week of my PHP/MySQL class at school and this weeks assignment is to have us create a game that a user can guess the title of a movie. There is only one answer so I don't have to deal with an array or anything like that. My question is what sting function(s) do I use if I want to compare a letter a user enters to letters that are in the answer. Then which string function(s) do I use to display the correct letters guessed with the remaining letters shown as asterisks. I don't want anyone to write the code for me I am just trying to be pointed down the right path. I am going to include what I have already written. I would appreciate any advice or guidance. Thank you.

Oops, something didn't work out there for the code blocks.
So, like hangman, but no limit?
First, having the $GameAnswer passed through the form itself kind of defeats the purpose. Since it's harcoded in script anyway, I'd suggest leaving it as that and not providing it to the user. Just compare what you have been given so far.
Which leads to the question of tracking the user entries so far. HTTP itself is stateless, so we need to persist the data in another fashion. Options are either to use sessions, or to pass them through the form.
Have you covered sessions? Have you covered arrays?

PHP Code:

header('HTTP/1.1 420 Enhance Your Calm');

Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

I have covered arrays and that was what I was thinking about using in order to display the answer with the correct letter and asterisks after each guess. I don't understand how you compare what the user types in to the letters in the answer. One of the stipulations of the assignment is to display after each attempt so the user can see the remaining letters.

There are a few different ways to check the selected items to the given string. Given a learning process, I'd suggest iteratively and using an array to contain the answers already given by the user.
You can iterate a string as it is simply an char array. It is mutable in PHP as well, so you can modify it iteratively.

Now during iteration, you can check the char against an array of user selected chars. If $sWord[$i] is in the selected array of chars, then show that letter. Otherwise, show *. You may want to push the space char as well otherwise you won't be able to see the spaces.
There are several ways to see if a value is in an array. Iterative is one, but there are functions for this as well. See here for a list of all the array functions: http://www.php.ca/manual/en/book.array.php
Finally, you need to figure out a way to persist previously selected chars. If you know sessions, that's a simple matter of creating an array within the sessions that has these. If not, you can pass an array through a hidden field via serialize() and unserialize() to read and write them. Then you simply add the newly provided char to the array of chars passed, and when they are presented with the form again you can provide it with the array serialized again. This way you get to always keep it in an array.

Add some simple logic beyond there to indicate that all letters were chosen, and even to indicate when a letter has already been chosen.

Hopefully that gives you some hints where to go. Like I said, there are several other options as well for you. str_[i]replace does accept arrays, but you'll need to reverse the logic in order to set *'s where there is no corresponding match as opposed to what is set. range() can be used to populate a list of chars a-z for example, and array_diff functions can be used to pull things out. This is actually a better solution overall, but doesn't have any looping involved, so I guess that depends on if you are doing an assignment for something like looping.

PHP Code:

header('HTTP/1.1 420 Enhance Your Calm');

Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)