hamburgerlove413 Posted October 6, 2013 Share Posted October 6, 2013 (edited) hello, I'm trying to create a simple word guessing game but I'm having a problem where when I load in a text file, when I put the lines into an array, it adds 2 extra empty spots to the end of the array, so when I display the placeholders for the phrase to be guessed, there are two extras. Anyone have any idea of where I'm going wrong? The code is below and I'm also attaching the text file in case theres a problem there. Also theres a next phrase button thats there along with some code for it but I don't have that working yet. I don't think that's related to my problem though. <?php session_start(); ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>Project 2</title> <link href="css/layout.css" rel="stylesheet" type="text/css"> </head> <body> <div id="container" class="clearfix"> <?php print "<form method='post'>"; function inputscreen($guesses) { print "<span>Guess a letter!</span>"; print "<span> You have ". $guesses . " guesses left! </span>"; print "<input type='text' name='guess' />"; print "<input type='submit' name='action' value='GUESS' />"; print "<input type='submit' name='action' value='REVEAL' />"; print "<input type='submit' name='action' value='NEXT PHRASE'>"; print "</form>"; } function printTable($letterCount, $visibleArray, $wordArray) { // print table for word display print "\n \n <table> \n \n \t <tr>"; for ($i = 0; $i < $letterCount; $i++) { if ($visibleArray[$i] == 1) { print "\n \n \t \t <td>" .$wordArray[$i]. "</td> \n"; } else if ($wordArray[$i] == " ") { print "\n \n \t </tr> \n \n</table> \n \n <table> \n \n \t <tr>"; } else { print "\n \n \t \t <td> </td>"; } } print "\n \n \t </tr> \n \n </table> \n \n"; } //text loader function textLoader($whatfile) { if(!file_exists($whatfile)) { print "Sorry, can't find " . $whatfile; exit; } else { $textarray= file($whatfile); return $textarray; } } // global variables $theText = textLoader('words.txt'); $action = $_POST['action']; $team = $_SESSION['team']; $visibleArray = $_SESSION['visible']; $guesses = $_SESSION['guesses']; $word = $theText[0]; $wordArray = str_split($word); $letterCount = count($wordArray); if (!isset($action)) { $guesses = 5; for ($i = 0; $i < $letterCount; $i++) { $visibleArray[$i] = 0; } } else if ($action == "GUESS") { $guess = strtoupper($_POST['guess']); if (preg_match("/^[a-zA-Z]$/", $guess)) { if (in_array($guess, $wordArray)) { for ($i=0; $i < $letterCount; $i++) { if ($guess == $wordArray[$i]) { $visibleArray[$i] = 1; } } print "<span>You guessed the letter " .$guess . " and are correct! </span>"; } else { print "<span>Nope! Try Again!</span> "; $guesses -= 1; } } else { print "<span>You need to guess a letter!</span>"; } } else if ($action == "REVEAL") { for ($i =0; $i < $letterCount; $i++) { $visibleArray[$i] = 1; } print "<span>The phrase was </span>"; print "<div class='reveal'>" . $word . "</div>"; print "<input type='submit' name='action' value='NEXT PHRASE'>"; } else if ($action == "NEXT PHRASE") { $nextPhrase++; for ($i = 0; $i < $letterCount; $i++) { $visibleArray[$i] = 0; } } inputscreen($guesses); printTable($letterCount, $visibleArray, $wordArray); $_SESSION['visible'] = $visibleArray; $_SESSION['team'] = $team; $_SESSION['guesses'] = $guesses; ?> </div> </body> </html> words.txt Edited October 6, 2013 by hamburgerlove413 Quote Link to comment https://forums.phpfreaks.com/topic/282765-inserting-text-files-into-arrays/ Share on other sites More sharing options...
.josh Posted October 7, 2013 Share Posted October 7, 2013 There are probably a couple of linebreaks at the end of the file, and possibly other blank spaces thrown into the mix. Try using array_map with trim and array_filter on the array. Example: $array = array_filter(array_map('trim',$array)); This will remove any extra spaces and tabs from each array element, and also remove any empty array elements. Quote Link to comment https://forums.phpfreaks.com/topic/282765-inserting-text-files-into-arrays/#findComment-1452881 Share on other sites More sharing options...
hamburgerlove413 Posted October 7, 2013 Author Share Posted October 7, 2013 (edited) Hello, Thanks for the reply! This unfortunately didn't work. There are still two extra spaces on the end of the array even after trying that. I checked the text file and there doesn't seem to be any extra spaces anywhere. I can fix this by subtracting two from $letterCount but I'd really like to know what's going on and why it's doing that. Edited October 7, 2013 by hamburgerlove413 Quote Link to comment https://forums.phpfreaks.com/topic/282765-inserting-text-files-into-arrays/#findComment-1452887 Share on other sites More sharing options...
Solution Ch0cu3r Posted October 7, 2013 Solution Share Posted October 7, 2013 Where did you apply .josh's example code? This is the line that needs to be modified for .josh's example to work $textarray = file($whatfile); So it becomes $textarray = array_filter(array_map('trim', file($whatfile))); // OR you could set the FILE_IGNORE_NEW_LINES and FILE_SKIP_EMPTY_LINES flags for file() $textarray = file($whatfile, FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES); Quote Link to comment https://forums.phpfreaks.com/topic/282765-inserting-text-files-into-arrays/#findComment-1452908 Share on other sites More sharing options...
hamburgerlove413 Posted October 7, 2013 Author Share Posted October 7, 2013 ah, ok. Yea, that fixed it. Thanks ! Quote Link to comment https://forums.phpfreaks.com/topic/282765-inserting-text-files-into-arrays/#findComment-1452923 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.