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>";
	}


}

}

Link to comment
Share on other sites

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.