JsusSalv Posted November 17, 2008 Share Posted November 17, 2008 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 More sharing options...
ratcateme Posted November 17, 2008 Share Posted November 17, 2008 sop there is whitespace at the beginning or ending. can you give us an example of th output you are getting and the output you expect Scott. Link to comment https://forums.phpfreaks.com/topic/133043-solved-trim-whitespace-from-db-query/#findComment-691921 Share on other sites More sharing options...
JsusSalv Posted November 17, 2008 Author Share Posted November 17, 2008 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. Link to comment https://forums.phpfreaks.com/topic/133043-solved-trim-whitespace-from-db-query/#findComment-691923 Share on other sites More sharing options...
ratcateme Posted November 17, 2008 Share Posted November 17, 2008 trim will only remove from the beginning and ending. look at str_replace() Scott. Link to comment https://forums.phpfreaks.com/topic/133043-solved-trim-whitespace-from-db-query/#findComment-691924 Share on other sites More sharing options...
DarkerAngel Posted November 17, 2008 Share Posted November 17, 2008 str_replace(" ", "", trim(htmlentities($element['element_name'])) Link to comment https://forums.phpfreaks.com/topic/133043-solved-trim-whitespace-from-db-query/#findComment-691925 Share on other sites More sharing options...
JsusSalv Posted November 17, 2008 Author Share Posted November 17, 2008 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! Link to comment https://forums.phpfreaks.com/topic/133043-solved-trim-whitespace-from-db-query/#findComment-691928 Share on other sites More sharing options...
JsusSalv Posted November 17, 2008 Author Share Posted November 17, 2008 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. Link to comment https://forums.phpfreaks.com/topic/133043-solved-trim-whitespace-from-db-query/#findComment-691929 Share on other sites More sharing options...
JsusSalv Posted November 17, 2008 Author Share Posted November 17, 2008 http://us.php.net/manual/en/function.trim.php "This function returns a string with whitespace stripped from the beginning and end of str " That answers my question. trim() works on a string and not on individual words. Link to comment https://forums.phpfreaks.com/topic/133043-solved-trim-whitespace-from-db-query/#findComment-691930 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.