Jump to content

trouble using trim() with an array from explode?


galvin

Recommended Posts

Ok, this was driving me nuts.  I think I found the cause, but need help fixing it.  I have a bunch of answers in a MySQL database in the form of...

 

Miami Heat/heat/miami

Orlando Magic / orlando/ magic

etc.

etc.

 

*Notice how sometimes there are leading or ending spaces (i.e there is a space before "magic" but NOT a space before "miami")...

 

I run this code to bring back the answers data (all acceptable answers) in an array.  I then look for the user entered answer ($userinput, which has trim and strtolower run on it) in the array.

 

Using this code below, the answer IS found if $userinput is any of the Miami Heat options, but it is NOT found if it's any of the Orlando Magic options.  The only difference is the fact that there are spaces in the Orlando Magic stuff.  Apparently, the trim() function is only removing space from the beggining and end of the ENTIRE string.  But I need to trim() each item in the array. How can I do this?  Seems like it should be easy but can't figure it out.

 

$sql = "SELECT * FROM answers
WHERE quizid = $quizid";
$result = mysql_query($sql, $connection);
if (!$result) { 
die("Database query failed: " . mysql_error());
} else {
while ($getanswers = mysql_fetch_array($result)) {

	$array = explode("/", strtolower(trim($getanswers['answer'])));

	if (in_array($userinput,$array)) {
	echo "the answerid = " . $getanswers['answerid'] . "<br>";
	} else {
	echo "the answerid is not found= <br>";
	}


}

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.