Jump to content

Recommended Posts

I'm using the following code to attempt to strip white space from my search results:

 

while($row = mysql_fetch_assoc($rs)) {

trim($row['Name']);
trim($row['Address']);
trim($row['City']);
trim($row['State']);
trim($row['Zip']);
trim($row['Phone']);

  
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Address'] . "</td>";
echo "<td>" . $row['City'] . "</td>";
echo "<td>" . $row['State'] . "</td>";
echo "<td>" . $row['Zip'] . "</td>";
echo "<td>" . $row['Phone'] . "</td>";
echo "<td>" . $row['Type'] . "</td>";

 

Obviously, I'm leaving out a ton of code, but the important part is that it's not stripping the white space.  Things like this make me want to pound spikes into my eyes, because there's just no logical way for me to work though this.  I'm extremely OCD, and so if code doesn't work the way the documentation states, I freeze.   

 

I also tried (as per another tutorial example) setting a second variable trimming it, and using the second variable in the Echo, like so:

 

$Tname = ($row['Name']);
echo "Blah blah blah" . trim($Tname);

 

That didn't work either.  It's simply NOT removing the white space, despite the fact that the entire description of what "Trim" does consists of "removes white space."  It boggles my mind.  Am I supposed to sacrifice a goat first or something? :)

 

BTW, if I go in and edit the database manually, I can strip out the spaces by hand, and everything works fine.  But it seems to me like the Trim command is supposed to exist to make it so that I don't have to do that.  The only thing I can guess is that for some reason the contents of the DB aren't being seen as Strings, even though they are, in fact, Strings.  I'm able to maniupulate them as Strings everywhere else in the code.  Just not here.  :(  Does anyone know what I did wrong?

Thanks!

Kyle

Link to comment
https://forums.phpfreaks.com/topic/254176-trim-not-actually-trimming/
Share on other sites

It's simply NOT removing the white space, despite the fact that the entire description of what "Trim" does consists of "removes white space."

 

It removes leading and trailing whitespace. It will not remove whitespace within a string.

 

You aren't actually assigning the result of the trim function to anything. Also, if you're going to trim all the elements, you just use array_map.

$row = array_map( 'trim', $row );

... and you might want to do this in your query, if your intent is to UPDATE the data with the trimmed value - http://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_trim

 

No need to select data, loop through it, and update it. Just UPDATE it all at once with the trimmed value.

In the code you posted, you are trimming the data of white space from either side, but you are not assigning the trimmed value to anything.

 

Try this:

while($row = mysql_fetch_assoc($rs)) {
echo "<td>" . trim($row['Name']) . "</td>";
echo "<td>" . trim($row['Address']) . "</td>";
echo "<td>" . trim($row['City']) . "</td>";
echo "<td>" . trim($row['State']) . "</td>";
echo "<td>" . trim($row['Zip']) . "</td>";
echo "<td>" . trim($row['Phone']) . "</td>";
echo "<td>" . trim($row['Type']) . "</td>";
}

 

Or if you are wanting to manipulate data after echoing you could in fact use array_map to trim each $row element like this:

while($row = mysql_fetch_assoc($rs)) {
        //### Apply trim to each $row element
        $row = array_map('trim', $row);

echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Address'] . "</td>";
echo "<td>" . $row['City'] . "</td>";
echo "<td>" . $row['State'] . "</td>";
echo "<td>" . $row['Zip'] . "</td>";
echo "<td>" . $row['Phone'] . "</td>";
echo "<td>" . $row['Type'] . "</td>";
}

 

Regards, PaulRyan.

... and you might want to do this in your query, if your intent is to UPDATE the data with the trimmed value - http://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_trim

 

No need to select data, loop through it, and update it. Just UPDATE it all at once with the trimmed value.

 

See, this is why I love you guys.    :) 

 

Oh,  and just to clarify: sometimes I forget to explain all the steps that got me from Point "A" to... still at Point "A".  :)  In regards to assigning the value, doesn't the second code group do that?  In other words, in this code:

 

$Tname = ($row['Name']);
echo "Blah blah blah" . trim($Tname);

 

Isn't the Trimmed results inherently implied?  trim($Tname) should return the Trimmed results, no?  (And to clarify some more: I realize that it's only supposed to remove the white space from the beginning and the end.  That's what I need it to do- The results are currently coming back NOT in alphabetical order, because some of them have spaces at the beginning of the string.)

 

In any event, you guys supplied me with three ways to fix my current problem.  :D  Thank you very much!

 

 

EDIT: I'm beginning to think that there's a different problem at play here.  It's still not ordering correctly, and my guess is that the person who did the data entry copied something in there that's not technically "white space."  I think Trim was working as advertised all along, but whatever is in there invisibly is what's screwing up my Query.  Originally I was going to post a link to the problem page, but then I realized I'd have fixed it manually before anyone could check it out.  :D

Thanks again!

It's your query that is ordering the data, therefore the data itself must be trimmed for the ORDER BY term in the query to be able to order all of it correctly.

 

Doing this in php code would only be useful if you were selecting the data, looping through it, then updating the table with the trimmed result, which as already mentioned, is not needed. You can directly UPDATE the table with the trimmed result in one UPDATE query.

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.