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
php file read problem
Moderator: General Moderators
feyd | Please use
The result is:
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]
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!\nCode: 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);
?>Code: Select all
Hello World!\nThis is test!\nHello 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]feyd | Please use
Looking at page source
if you're file is just
where \n is text rather than a newline
try this
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>";
}
?>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>Code: Select all
hello\nwelcome to the system\nblah blah\nblah\ntry 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]