Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Regular Coder
    Join Date
    Jan 2009
    Posts
    173
    Thanks
    5
    Thanked 12 Times in 11 Posts

    Question Splitting each letter in a word into an array and replacing them with an underscore?

    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:

    PHP Code:
    //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:
    PHP Code:
    $_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

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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.

  • #3
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    http://www.php/str_split
    PHP Code:
    $word  'banana';
    $chars str_split$word ); // Array( 0 => b, 1 => a, 2 => n, ... ) 
    To handle the underscore replacements:
    PHP Code:
    $word  'banana';

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

    $word  str_replace$chars'_'$word );

    echo 
    $word// __n_n_ 
    Last edited by kbluhm; 09-04-2009 at 04:59 PM.

  • #4
    Regular Coder
    Join Date
    Jan 2009
    Posts
    173
    Thanks
    5
    Thanked 12 Times in 11 Posts
    @Fumigator
    Ahaaa, that's cool. I never knew about that.

    Thanks for the super fast responds too btw

    I'll try it out and post back!

  • #5
    Regular Coder
    Join Date
    Jan 2009
    Posts
    173
    Thanks
    5
    Thanked 12 Times in 11 Posts
    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?
    Last edited by nikee; 09-04-2009 at 05:03 PM.

  • #6
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Quote Originally Posted by nikee View Post
    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.

  • #7
    Regular Coder
    Join Date
    Jan 2009
    Posts
    173
    Thanks
    5
    Thanked 12 Times in 11 Posts
    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:
    ______

  • #8
    Regular Coder
    Join Date
    Jan 2009
    Posts
    173
    Thanks
    5
    Thanked 12 Times in 11 Posts
    Nobody?

  • #9
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    I don't know PHP but if you make some modifications in the code of kbluhm you can do this:
    Code:
    $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.
    Software and cathedrals are much the same - first we build them, then we pray.

  • #10
    Regular Coder
    Join Date
    Jan 2009
    Posts
    173
    Thanks
    5
    Thanked 12 Times in 11 Posts
    Thanks. Is there any way to add a space between each char when you echo it?

    Like this...:

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

  • #11
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    try this:
    Code:
    $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??
    Software and cathedrals are much the same - first we build them, then we pray.

  • #12
    Regular Coder
    Join Date
    Jan 2009
    Posts
    173
    Thanks
    5
    Thanked 12 Times in 11 Posts
    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 I appreciate it.
    -Nike


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •