Jump to content

remove white space from variable


chanchelkumar

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

  • 2 weeks later...
  • 2 months later...
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.