Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extra Line Breaks... 1

Status
Not open for further replies.

Programmer76

Programmer
Jul 25, 2006
35
CA
Hi,

I am trying to create a csv file using fwrite and the file is created no problem.

I am simply looping my MySQL recordset and entering it into the file as follows:

Code:
$strData = $row['MyID'] . "|" . $row['TextInfo'];
fwrite($FileHandle, $strData);

It looks like the field TextInfo contains some extra cariage returns or line breaks and this is messing up the csv file when I import it into Excel.

My guess is that someone has edited the field through PHPMyAdmin and hit the enter button. The problem is that you can't see an acual new line character o anything...

Any ideas how I can deal with this?

CES
 
Thanks for the suggestion. I gave it a try but but the thing is that those characters are not in the variable.

Here is an example of the csv that would be produced:

Code:
4895|Blah BlahBlah BlahBlah BlahBlah BlahBlah BlahBlah Blah
4896|Blah BlahBlah BlahBlah BlahBlah BlahBlah BlahBlah Blah
4947|Blah BlahBlah BlahBlah 
BlahBlah BlahBlah BlahBlah Blah
4897|Blah BlahBlah BlahBlah BlahBlah BlahBlah BlahBlah Blah
4898|Blah BlahBlah BlahBlah BlahBlah BlahBlah BlahBlah Blah

For item 4947 there is an extra line and if I look at the field in the MySQL database you can see that there s a new line but no special character like \n \r or <br> that causes it...

If anyone does not understand please ask, I am really stumped. Generally I am alright at debugging but I am really stumped here.

Any other suggestions? I am sure someone has come accross this before.

CES
 
There has to be some character there causing the line breaks.

If record 4947 has offending data, write a throwaway debugging script that fetches just that record and writes to a file, one value per line, the ASCII value of every character in that field.



Want the best answers? Ask the best questions! TANSTAAFL!
 
I absolutely made one of those "Tired Mistakes". Using the str_replace does fix this and I am not sure why I didn't think of that as I think I have done that in the past.

You know its time for a break when.

Thanks a lot.

Have a good one!

CES
 
The key is [tt]rtrim()[/tt] ...

Code:
$strData = $row['MyID'] . "|" . $row['TextInfo'];
fwrite($FileHandle, [COLOR=red]rtrim[/color]($strData));

Regards


Jakob
 
.... AND just to be on the safe side, this would be even better :)

Code:
$strData = [COLOR=red]rtrim[/color]($row['MyID']) . "|" . [COLOR=red]rtrim[/color]($row['TextInfo']);
fwrite($FileHandle, $strData);

Regards


Jakob
 
Nice.

Thanks for all the suggestions. Much appreciated.

CES
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top