chanchelkumar Posted December 7, 2007 Share Posted December 7, 2007 Hi actually i am working with a script which pull data from database and display in excelsheet (CSV) , in which i want to receive the data with out white space or "blank" .. i tried with trim,preg_replace and also arrays for this... Am still in the circle..... please help me..... Thanks in advance..... Link to comment https://forums.phpfreaks.com/topic/80603-remove-white-space-from-variable/ Share on other sites More sharing options...
Aureole Posted December 7, 2007 Share Posted December 7, 2007 EDIT: Wait I just realized... I was wondering why when I used that example from the PHP Website it didn't remove whitespace from $var = ' test'; ......I need to know how to do this too. Link to comment https://forums.phpfreaks.com/topic/80603-remove-white-space-from-variable/#findComment-408746 Share on other sites More sharing options...
PHP_PhREEEk Posted December 7, 2007 Share Posted December 7, 2007 If you wish to remove ALL white spaces, you can use str_replace: <?php $sql = "SELECT `dataField` FROM `dataTable`"; if ( !$result = mysql_query($sql) ) { die('MySQL Error: ' . mysql_error()); } while ( list($data) = mysql_fetch_assoc($result) ) { $data = str_replace(' ', '', $data); // do whatever else } // code continues PhREEEk Link to comment https://forums.phpfreaks.com/topic/80603-remove-white-space-from-variable/#findComment-408750 Share on other sites More sharing options...
Aureole Posted December 7, 2007 Share Posted December 7, 2007 PHP_PhREEEk, that doesn't work for me... any ideas why? Link to comment https://forums.phpfreaks.com/topic/80603-remove-white-space-from-variable/#findComment-408753 Share on other sites More sharing options...
PHP_PhREEEk Posted December 7, 2007 Share Posted December 7, 2007 What doesn't work? PhREEEk Link to comment https://forums.phpfreaks.com/topic/80603-remove-white-space-from-variable/#findComment-408755 Share on other sites More sharing options...
kairno Posted December 7, 2007 Share Posted December 7, 2007 from http://www.php.net/preg_replace Example#5 Strip whitespace This example strips excess whitespace from a string. <?php $str = 'foo o'; $str = preg_replace('/\s\s+/', ' ', $str); // This will be 'foo o' now echo $str; ?> not sure if i understood you rignt, you want to insert the data you take from a database into a csv. the function works, tested it. now you can do a str_replace(" ","<your delimiter>","$string) and write it into the csv... if i understood wrong, please correct me Link to comment https://forums.phpfreaks.com/topic/80603-remove-white-space-from-variable/#findComment-408782 Share on other sites More sharing options...
Aureole Posted December 7, 2007 Share Posted December 7, 2007 kairno, the example you posted removes excess white-space it does not remove all white-space. PHP_PhREEEk, the code you posted, in paticular: $data = str_replace(' ', '', $data); ...doesn't work for me. It doesn't remove the white-space. Link to comment https://forums.phpfreaks.com/topic/80603-remove-white-space-from-variable/#findComment-408786 Share on other sites More sharing options...
PHP_PhREEEk Posted December 7, 2007 Share Posted December 7, 2007 <?php $data = ' list4 foo bar hello'; $data = str_replace(' ', '', $data); var_dump($data); Output: string(16) "list4foobarhello" PhREEEk Link to comment https://forums.phpfreaks.com/topic/80603-remove-white-space-from-variable/#findComment-408795 Share on other sites More sharing options...
kairno Posted December 7, 2007 Share Posted December 7, 2007 as i said, i didn't understand the problem.. sorry Link to comment https://forums.phpfreaks.com/topic/80603-remove-white-space-from-variable/#findComment-408810 Share on other sites More sharing options...
Aureole Posted December 7, 2007 Share Posted December 7, 2007 Ah! This works: $data = str_replace(' ', '', $data); This doesn't: str_replace(' ', '', $data); Now I understand, thanks a lot. Link to comment https://forums.phpfreaks.com/topic/80603-remove-white-space-from-variable/#findComment-408815 Share on other sites More sharing options...
chanchelkumar Posted December 10, 2007 Author Share Posted December 10, 2007 But this is working only for a single space.... my contents having lot of whitespace.... For example: ">new firstline Fifthline space contiues..... End of opace"; how to remove this space.. Link to comment https://forums.phpfreaks.com/topic/80603-remove-white-space-from-variable/#findComment-410905 Share on other sites More sharing options...
PHP_PhREEEk Posted December 11, 2007 Share Posted December 11, 2007 The code provided will remove all whitespace. Even if 20 spaces are next to each other (some sort of padding or whatever), the code provided removes each individual whitespace, so all 20 would be removed. PhREEEk Link to comment https://forums.phpfreaks.com/topic/80603-remove-white-space-from-variable/#findComment-412188 Share on other sites More sharing options...
Aureole Posted December 23, 2007 Share Posted December 23, 2007 Try... $data = str_replace(' ', '', $data); $data = str_replace('\n', '', $data); or something Link to comment https://forums.phpfreaks.com/topic/80603-remove-white-space-from-variable/#findComment-421728 Share on other sites More sharing options...
alexloh Posted March 20, 2008 Share Posted March 20, 2008 In case the problem has not been solved yet, or maybe someone new is facing a similar problem... Here is the code to remove excessive whitespace from a string. ereg_replace('[[:space:]]+', ' ', trim($str)); Details see code example at: http://www.thewebscripter.com/tutorial/code_examples/remove_whitespace.php Hope this helps. Alex Link to comment https://forums.phpfreaks.com/topic/80603-remove-white-space-from-variable/#findComment-496850 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.