Weird behaviour when writing to a file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chocobear
Forum Newbie
Posts: 2
Joined: Wed Jan 23, 2008 3:48 pm

Weird behaviour when writing to a file

Post by chocobear »

Alright so here's the deal.

I am working towards moving all of my companies inventory online to the Google Base service via their XML RSS Feed deal. I have created a PHP script to automatically write the RSS file for me, right? Right. Anyway, so Google requires you to tab-delimit the file, which you know, should be easy.

But for some reason, whenever I try to fwrite or fputs \t \n \r or any escape character it puts the actual characters \ and t into the file. So say the code is

Code: Select all

fwrite('<blah>grag</blah>\t', $handle);
the output in the file would be

Code: Select all

<blah>grag</blah>\t
instead of

Code: Select all

<blah>grag</blah><whitespace tab>
I am opening the file with the 'wb' option.
And unfortunately I am using PHP 4.2.5 (my webhost is a little outdated and is slowly working on upgrading their servers)

Any ideas on why it would be doing this?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Weird behaviour when writing to a file

Post by Christopher »

In PHP you need to use double-quotes to have the string parsed for escaped characters and variables. Single quotes will keep the string exactly as typed. Try:

Code: Select all

fwrite("<blah>grag</blah>\t", $handle);
(#10850)
chocobear
Forum Newbie
Posts: 2
Joined: Wed Jan 23, 2008 3:48 pm

Re: Weird behaviour when writing to a file

Post by chocobear »

Well, now I feel kinda dumb.

Thanks much, it works wonders.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Weird behaviour when writing to a file

Post by Christopher »

We all had that very same question once. Now you can answer it when others ask! :)
(#10850)
Post Reply