Jump to content

[SOLVED] trim whitespace from db query


JsusSalv

Recommended Posts

Hello Everyone:

 

I'm trying to trim the whitespace from records returned from a query.  Here's my code:

 

while($element = @ mysql_fetch_array($result))
{
     echo '<div class="'.trim(htmlentities($element['element_name'])).'">'.$element['content'].'</div>';
}

 

I've tried switching the trim and htmlentities but nothing will remove the whitespace from $element['element_name'].

 

I may just use TRIM() within the db query itself but I'd like to keep this type of functionality within PHP.  Can someone shed some light as to what I'm doing wrong here?  Thanks!

Link to comment
https://forums.phpfreaks.com/topic/133043-solved-trim-whitespace-from-db-query/
Share on other sites

Output:

<div class="main content">Test1</div>
<div class="comment box">Test2</div>
<div class="advertisements">Test3</div>
<div class="articles">Test4</div>
<div class="flyer">Test5</div>

 

I need to remove the whitespace from within the words in the class names.  So it should look like this:

<div class="[b]maincontent[/b]">Test1</div>
<div class="[b]commentbox[/b]">Test2</div>
<div class="advertisements">Test3</div>
<div class="articles">Test4</div>
<div class="flyer">Test5</div>

 

Obviously, one word class names are no problem...it's the multiple-word class names that I'd like to trim.

Awesome!!  I had thought about str_replace but didn't try it out.  I thought I could get away with a simple trim().  I went with str_ireplace() and that worked perfectly.  Thank you for the suggestion.

 

Does anyone know why trim() doesn't work in the fashion I had attempted? Just being curious about it.  Thanks!

Thanks Scott for the answer in the previous post: "trim will only remove from the beginning and ending."

 

I guess my question is: Does trim() treat 'main content' as one term and since there is no whitespace on either end it doesn't do anything?  I thought trim() worked by taking each word with the string and removed the whitespace from either side of the words. 

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.