PHP - writing to a new line in a text file [duplicate] - php

I have the following test script:
<?php
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Floppy Jalopy\n";
fwrite($fh, $stringData);
$stringData = "Pointy Pinto\n";
fwrite($fh, $stringData);
fclose($fh);
?>
when run however and opened usign Notepad, the data is returned in a single line without breaks as:
Floppy Jalopy(crazy box)Pointy
Pinto(crazy box)
where i cant find the appropriate character for 'crazy box' but its a REALLY crazy box. WHAT GIVES!

It is best to use PHP_EOL. This is cross-platform, so it automatically chooses the correct newline character(s) for the platform PHP is currently running on.
$stringData = "Floppy Jalopy" . PHP_EOL;
PHP Constants

If you want to open the file in Windows notepad, you must use Windows line breaks: \r\n

Your code runs fine.
Use Notepad2 or Notepad++ if you're working on Windows. The built-in Notepad is unable to cope with Unix-style line endings.

. PHP_EOL; will work universally

Related

generating a text file in php with new lines [duplicate]

I have the following test script:
<?php
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Floppy Jalopy\n";
fwrite($fh, $stringData);
$stringData = "Pointy Pinto\n";
fwrite($fh, $stringData);
fclose($fh);
?>
when run however and opened usign Notepad, the data is returned in a single line without breaks as:
Floppy Jalopy(crazy box)Pointy
Pinto(crazy box)
where i cant find the appropriate character for 'crazy box' but its a REALLY crazy box. WHAT GIVES!
It is best to use PHP_EOL. This is cross-platform, so it automatically chooses the correct newline character(s) for the platform PHP is currently running on.
$stringData = "Floppy Jalopy" . PHP_EOL;
PHP Constants
If you want to open the file in Windows notepad, you must use Windows line breaks: \r\n
Your code runs fine.
Use Notepad2 or Notepad++ if you're working on Windows. The built-in Notepad is unable to cope with Unix-style line endings.
. PHP_EOL; will work universally

Can't add line break while storing string to a txt file in PHP

Simple code to learn how to open, write to a txt file and read it. I am having two issues.
I can't seem to add an actual line break while storing string in .txt file.
Echo is NOT printing the text in the browser
Code
<html>
<body>
<?php
$myfile=fopen("testfile.txt", "a") or die("unable to open file");
$txt=("john doe<br>");
fwrite($myfile, $txt);
$txt=("jane doe<br>");
fwrite($myfile,$txt);
echo fread($myfile, filesize("testfile.txt"));
?>
</body>
</html>
contents of .txt file ( as you can see it is literally adding line break tag in the txt file instead of adding a break)
john doe<br>jane doe<br>
Also please suggest why the echo is not printing when I run the PHP file in Chrome.
For the first part
The proper code would be
<html>
<body>
<?php
$myfile=fopen("testfile.txt", "a") or die("unable to open file");
$txt=("john doe\n");
fwrite($myfile, $txt);
$txt=("jane doe\n");
fwrite($myfile,$txt);
echo fread($myfile, filesize("testfile.txt"));
?>
</body>
</html>
<br> is a html tag and is rendered only in browser, for files you should use carriage return which is \n
As far as second question goes, i think that you are trying to open the .php file directly in browser, That is not the proper way to do that. Browser can only run js files. The php files run on your server, (usually apache/nginx). You can find a lot of manuals on how to configure them with php and the view the rendered output in browser.
For line break you are using <br> tag, it's line break for HTML. To write new line in file you can use PHP_EOL. It's PHP constant that adds \n for UNIX alike system and \r\n for windows, resulting line break in the file.
// Concat new line to string
$txt = "john doe".PHP_EOL;
fwrite($myfile, $txt);
// Concat new line to string
$txt = "jane doe".PHP_EOL;
fwrite($myfile, $txt);

Inexplicable linebreaks in xml file that cannot be removed

So I have a sql database that generates an xml file. This xml file gets put on a webserver and then parsed by a php file. Unfortunately randomly sometimes there will be a line break in the xml file that causes it to be unable to be parsed.
http://imgur.com/jNgiE
As you can see from line 12-14. There is a linebreak. But I have no idea why. And I even wrote a script to remove carriage returns and newline characters but the linebreaks still remain. Anyone have any ideas?
$inputXML = file_get_contents("ukso.xml");
$fixedXML = str_replace("\r","",$inputXML);
$fixedXML = str_replace("\n","",$inputXML);
$fixedXML = str_replace(" ","",$inputXML);
$myFile = "ukso.xml";
print $fixedXML;
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $fixedXML);
fclose($fh);
Your code to remove linebreaks is broken. make the following changes:
$inputXML = file_get_contents("ukso.xml");
$fixedXML = str_replace("\r","",$inputXML);
$fixedXML = str_replace("\n","",$fixedXML); // note: reference the correct variable here
$fixedXML = str_replace(" ","",$fixedXML); // and here.
$myFile = "ukso.xml";
print $fixedXML;
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $fixedXML);
fclose($fh);
The code as you have it is only trimming out double spaces.
I see you're using notepad++
Go into view-show symbol-show all characters. That will tell you exactly what characters there are in the file.
It's possible that the line breaks are also due to the line being too long otherwise. XML files are usually designed to have line breaks at the end of tags, and you may be running into a situation where the line length is too long somewhere in one of the programs. (Often they create a fixed buffer and read in as much as possible, and if it's too long the program will chop the line)

How do I make my shell_exec output into pages?

Hi I am new to PHP and hope someone can help me.
Sometimes the output from the executed shell script can be hundreds of lines long, the lines are separated with a <BR> (replaced \n in shell script) for formatted html output.
So I need to know how to make the output paginated, I looked at some other similar solutions here but I couldn't make them work as they did different things.
$url = $_POST["website"];
$safeurl = escapeshellarg($url);
#passthru("./check -n ".$safeurl);
$stuff=shell_exec("./webcheck -n ".$safeurl);
$webFile = ($url.'.txt');
$write = $stuff;
$fh = fopen($webFile, 'w') or die("can't open file");
fwrite($fh, $write);
fclose($fh);
$fh = fopen($webFile, "r") or die("can't open file");
$frstuff=fread($fh, filesize($webFile));
fclose($fh);
echo $frstuff;
If you try using exec with an additional parameter instead of shell_exec, you can get the output lines as an array rather than one long string.
$output = array();
exec("./webcheck -n $safeurl", $output);
// Inspect the contents of $output
var_dump($output);
Then you can iterate through that array ($output) as needed.
Its not the best solution, but the easiest is going to be using javascript to do the pagination.
Try: http://plugins.jquery.com/project/pagination
Otherwise you could plugin a generic database pagination script and change the database adaptor to point to a file adaptor, where number of rows becomes number of lines.
Edit: provided better link

PHP New Line will not work

Hi I am trying to create some code that first reads the existing contents of the file in and then adds the new line of code on a new line but the code i am using just adds it on the new text on to the already existing line instead of the new line...
Here is the code i am using:
<?php
$id = $_GET['id'];
$userfile = "user1.txt";
$fo = fopen($userfile, 'r') or die("can't open favourites file");
$currentdata = fread($fo, filesize($userfile));
fclose($fo);
$fw = fopen($userfile, 'w') or die("can't open favourites file");
$currentprocessed = "$currentdata\n";
fwrite($fw, $currentprocessed);
fwrite($fw, $id);
fclose($fw);
?>
I have tried a whole range of different ideas but nothing has worked, any help would be appreciated.
Line endings per OS
Unix / Linux
\n
DOS / Windows
\r\n
Invalid
\r and \n\r
The value of PHP_EOL constant depends on the platform php is running on.
It doesn't detect the line-endings in the current file or anything magic.
Instead of appending \n, concatenate the constant PHP_EOL which is always the correct newline character for the current platform.
It might also be an issue with the program you're using to open the text file with. For instance, Notepad on Windows is incapable of understanding unix style newlines.
I ran into this same issue. What application are you using to read the file? I found that for some reason Notepad (my default for .txt files) didn't recognize the "\n\r" escape characters. I opened my .txt file that I was writing to using Notepad++, Atom (my text editor of choice), or in a browser and they all showed the line breaks just fine.

Categories