Page 1 of 1

php file read problem

Posted: Mon May 01, 2006 10:19 am
by Anet
Hello all,
I am trying to achieve something very simple, but was not able to. Any help will be appreciated.
Suppose I have a txt file with: "Hello World!\nHow are you today?\n" content. Is it possible to "echo" the content
making php actually interpret "\n" as new line?
Thanks

Posted: Mon May 01, 2006 10:32 am
by hawleyjr

Posted: Mon May 01, 2006 10:41 am
by Anet
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I have tried that. Does not help. The string which is read from file does not contain actual new line, only the \n string. Like this:
Hello World!\nThis is test!\n

Code: Select all

<?php
$ic = 0;
$ic_max = 100;  // stops after this number of rows
$handle = fopen("F:\\PhpWeb\\myfile.txt", "r");
while (!feof($handle) && ++$ic<=$ic_max) {
   $buffer = fgets($handle, 4096);
   echo $buffer;
}
fclose($handle);
?>
The result is:

Code: Select all

Hello World!\nThis is test!\n
I would like to be:
Hello World!
This is test!


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon May 01, 2006 11:00 am
by savant
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Code: Select all

<?
if(($string=file_get_contents("file.txt"))==true){
  echo "Output before: $string<br>";
  echo "Output now: ".nl2br($string)."<br>";
}else{
  echo "can't open file<br>";
}
?>
Looking at page source

Code: Select all

Output before: hello
welcome to the system
blah blah
blah

<br>Output now: hello<br />
welcome to the system<br />
blah blah<br />
blah<br />
<br />
<br>
if you're file is just

Code: Select all

hello\nwelcome to the system\nblah blah\nblah\n
where \n is text rather than a newline

try this

Code: Select all

$string=str_replace('\n',"<br>",$string);

or

$string=str_replace("\\n","<br>",$string);

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon May 01, 2006 11:07 am
by Anet
Yes, you are right! Thanks a lot!