Null characters and text files help (n00b alert)

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
mcmikemc
Forum Newbie
Posts: 2
Joined: Wed Feb 13, 2008 10:54 am

Null characters and text files help (n00b alert)

Post by mcmikemc »

I am trying to create a script that will create a text file with a list of information. In my code I put a \n into a string every time I want a new line to start. For most doc readers this works fine but when I try to open the file I create in Notepad (The one that comes with Windows) there are square characters where line breaks should be.

Does anybody know how to make it so this does not happen?

Code: Select all

$email_list_string .= "$variable1";
$list_string .= "; ";
$list_string .= "$variable2";
$list_string .= "; ";
$list_string .= "$variable3";
$list_string .= "; \n";
 
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $list_string);
fclose($fh);
 
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Re: Null characters and text files help (n00b alert)

Post by liljester »

for windows use "\r\n" for new lines
mcmikemc
Forum Newbie
Posts: 2
Joined: Wed Feb 13, 2008 10:54 am

Re: Null characters and text files help (n00b alert)

Post by mcmikemc »

It works :D

Thanks
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Null characters and text files help (n00b alert)

Post by Chris Corbyn »

For portability, use PHP_EOL for newlines ;)

Code: Select all

$email_list_string .= "$variable1";
$list_string .= "; ";
$list_string .= "$variable2";
$list_string .= "; ";
$list_string .= "$variable3";
$list_string .= "; " . PHP_EOL;
 
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $list_string);
fclose($fh);
Post Reply