Forum Moderators: coopster

Message Too Old, No Replies

formating output of fwrite

         

graemes

7:04 am on Jan 9, 2006 (gmt 0)

10+ Year Member



I want to output lines, line by line to a text file
and am using fwrite with an array and a loop.

I want to place quotation marks around the word.

so the output would be like so:

"pig"
"dog"

but I am getting

"pig
""dog
""horse

my fwrite line is:

fwrite($fh,"" ."\"".$name[$i]."\"")

Thanks

vevs

9:41 am on Jan 9, 2006 (gmt 0)

10+ Year Member



this works fine

fwrite($fh,'"'.$name[$i].'"'."\n");

Vevs

coopster

3:44 pm on Jan 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, graemes.

vevs and you seem to be putting down the same code here, just formatted a bit differently. Without seeing the rest of our code, it seems perhaps your $names variables may contain a newline. Have you checked that yet? If you use vevs example are you getting two newlines between variables when you View Source?

vevs

3:47 pm on Jan 9, 2006 (gmt 0)

10+ Year Member



I am just putting the code that I've tested. My way it correctly puts the ", but I did not tested graemes code. Of course, as you stated, he does not need "\n" if there is new line in his words.

Vevs

coopster

4:40 pm on Jan 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Exactly. And looking at the sample output offered in the first message, I'm guessing that is the case. Thanks for testing it vevs.

graemes

9:45 pm on Jan 9, 2006 (gmt 0)

10+ Year Member



Yes, that's all I can put it down to.

I am sending the output to an email.

The $names array variable seems to output a newline.

I need to chomp it I think or chop it to make this work,
but being new to php I don't know how to fit the chop or rtrim command into my particular code.

I've just got to remove that pesky newline somehow and it's all good.

coopster

9:52 pm on Jan 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



It would be ideal to remove it from the $names array indexes if you don't want them there or don't need them (it depends on how you are filling up this array -- looks to me like you may be perhaps reading it from a text file or something).

However, to just do so using rtrim, just add it to your fwrite line. Using vevs snippet:

fwrite($fh,'"'.rtrim [php.net]($name[$i]).'"'."\n");

graemes

10:10 pm on Jan 9, 2006 (gmt 0)

10+ Year Member



Thanks Coopster and Vevs, by trimming the newline using rtrim and Vevs code the section of the script now works.

Great!