...

View Full Version : Splitting each letter in a word into an array and replacing them with an underscore?



nikee
09-04-2009, 04:52 PM
Hey. (again..)

I'm trying to create a simple hangman game in PHP. I've already had some problems with it but i've solved them.

What i need to know now is how to make each letter in a word into an array... like this:



//before it's in an array..
$word = 'banana';

//this is how i want it..
$word array('b', 'a', 'n', 'a', 'n', 'a');


I believe this could be done using a foreach command somehow? I hope i'm not terribly wrong now.

something like this:


$_SESSION['word'] array('b', 'a', 'n', 'a', 'n', 'a');
foreach ($_SESSION['word'] as $letter) {
$letter = str_replace($_SESSION['word'], '', '_');
}


I hope this makes any sence. :)

Thanks
-Nike

Fumigator
09-04-2009, 04:55 PM
Strings are actually arrays of characters so you don't need to do any conversion at all. Just reference by numeric index. For example if $word = 'banana', then $word[0] = 'b', $word[1] = 'a', etc.

kbluhm
09-04-2009, 04:55 PM
http://www.php/str_split


$word = 'banana';
$chars = str_split( $word ); // Array( 0 => b, 1 => a, 2 => n, ... )

To handle the underscore replacements:


$word = 'banana';

$chars = array( 'a', 'b' );

$word = str_replace( $chars, '_', $word );

echo $word; // __n_n_

nikee
09-04-2009, 04:59 PM
@Fumigator
Ahaaa, that's cool. I never knew about that.

Thanks for the super fast responds too btw :D

I'll try it out and post back!

nikee
09-04-2009, 05:01 PM
How would i do if i wanted to first split a word into an array (with each letter..) and then replace each array variable with an underscore and save that result in another variable?

kbluhm
09-04-2009, 05:04 PM
How would i do if i wanted to first split a word into an array (with each letter..) and then replace each array variable with an underscore and save that result in another variable?
If you want to split a word into characters, then replace each character with an underscore, you'll just end up with a bunch of underscores. :confused:

nikee
09-04-2009, 05:07 PM
Heheh, yeah i know. That's what i want to do.

I want to display an underscore for each letter... and then when a user submits a character from a form, the php script will check if that character exists anywhere in the word, and if it does, that character will be visible for the visitor.

So for example... if a user tries with an "a" then it will look like this:
_a_a_a

but by default it will look like this:
______

nikee
09-04-2009, 05:22 PM
Nobody?

ckeyrouz
09-04-2009, 05:26 PM
I don't know PHP but if you make some modifications in the code of kbluhm you can do this:


$word = 'whatever you want your word to be';

$chars = str_split( $word );

$word = str_replace( $chars, '_', $word );



I think this should do it, but there might be some redundant letters so I do not know how to optimize the str_split to not duplicate the letters inside the array.

nikee
09-04-2009, 05:34 PM
Thanks. Is there any way to add a space between each char when you echo it?

Like this...:

banana (normal) = ______
banana (with spaces) = _ _ _ _ _ _

ckeyrouz
09-04-2009, 05:45 PM
try this:


$word = 'whatever you want your word to be';

$chars = str_split( $word );

$word = str_replace( $chars, '_ ', $word );


But I think this will add a space at the end of you word.
Shouldn't be a problem, no??

nikee
09-04-2009, 05:51 PM
Heheh. That was such an easy solution. I can't believe i missed that, hehe.

No, that is not a problem. :)

To be honest. I don't think i'm gonna be able to finish this game. There's a lot left to do, and i'm asking you guys for help on every step, and that's pretty much cheeting. Or maybe not... i mean, i'm learning a lot from doing this, but it's not nice to ask for help on every step when i'm suppose to do it myself. That's almost like saying "Hey, do this for me!", and i don't want that.

I might try this another time, when i know a little bit more :)

Thanks for the help guys :D I appreciate it.
-Nike



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum