Jump to content

inserting text files into arrays


Go to solution Solved by Ch0cu3r,

Recommended Posts

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 by hamburgerlove413
Link to comment
https://forums.phpfreaks.com/topic/282765-inserting-text-files-into-arrays/
Share on other sites

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.

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 by hamburgerlove413
  • Solution

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);
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.