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..... Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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? Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted December 7, 2007 Share Posted December 7, 2007 What doesn't work? PhREEEk Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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.. Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.