Read text file and print in the same format - php

i want to read a text file on a php page and print (echo) the content in the same form (with the paragraphs). So i tried two implementations
Code 1
$textfile = "teste.txt"; // Declares the name and location of the .txt file
$content="";
$fileLocation = "$textfile";
$fh = fopen($fileLocation, 'w ');
if (is_readable($textfile)) {
$content .= fread($fh, 8192);
echo $content;
} else {
echo 'The file is not readable.';
}
fclose($fh);
?>
Using this code nothing apper on the screen. and i had another problem. If i used filesize ($textfile) on the fread i had this error Length parameter must be greater than 0.So i guess this is the problem. but the text file has content
Code 2
<?php
$homepage = file_get_contents('teste.txt');
echo $homepage;
?>
This code works. but the format is not what i want.
the echo prints this
25 ºC 26 ºC 27 ºC 26 ºC 26 ºC
I want that appear one value on each line like i have on the text file.
What can i do to acomplish that
Thanks for the help

note sure if this is answerd, but try this
<?php
$file = "LOCATION/NAME.txt";
$text = file_get_contents($file);
$text = nl2br($text);
echo $text;
?>

This should be as simple as wrapping the output inside the PRE tag:
<?php
$homepage = file_get_contents('teste.txt');
echo '<PRE>' . $homepage . '</PRE>';
?>
This will work better than nl2br() if you also need to keep the exact number of spaces due to needing a fixed-width font. HTML normally turns two or three spaces in a row into just one. But inside a PRE tag if you have 3 spaces, all 3 will be displayed.

Related

file_get_contents() skipping text between <> tag

I am trying to read a .tsv file using PHP. I am using the simplest method of file_get_contents() but it is skipping any text between <> tags.
Following is the format of my .tsv file
<id_svyx35_88c_avbfa5> <Kuldeep_Raval> rdf:type <wikicat_Delhi_Daredevils_cricketers>
Following is the code I am using
$filename = "access_s.tsv";
$content = file_get_contents($filename);
//Split file into lines
$lines = explode("\n", $content);
echo $content;
On reading it, the output is just
rdf:type
Please help in what can be the solution to read the line as it is?
Try to apply htmlspecialchars() to $content:
$filename = "access_s.tsv";
$content = htmlspecialchars(file_get_contents($filename));
//Split file into lines
$lines = explode("\n", $content);
echo $content;
Reference on php.net
The tags have always been there, the browser just does not show them. Just like with any valid HTML tag, you can see them when viewing the source code of the website.

Php putting amount of characters after string

I'm a front end developer but I'm learning php so I can do basic back end stuff. My code seems to be putting the amount of characters after the echoed string.
<?php
$openFile = fopen("file.txt","w");
$name = "Chris";
fwrite($openFile,$name);
fclose($openFile);
$openFile = fopen("file.txt","r");
echo readfile("file.txt");
fclose($openFile);
?>
This is what I get for posting here. -2.
Live preview: http://icodewebsitesandineedatestingserver.netai.net/
I guess you're trying to read the contents of the file?
PHP doc on readfile:
Reads a file and writes it to the output buffer.
5 is the number of bytes read.
You can use file_get_contents:
<?php
$openFile = fopen("file.txt","w");
$name = "Chris";
fwrite($openFile,$name);
fclose($openFile);
echo file_get_contents("file.txt");
?>

How to write file as it is?

I have a string of code like this:
<?php
echo "Hello World";
?>
Now, I want to write that into a php file just the way it is. What I mean is that I want to write this code with new line characters. So the code gets stored in the above fashion. So, how can I do that?
I had tried to use file append and fwrite, but they write the code in one consecutive line and what I want is to have it divided into 3 lines.
On the first line - <?php
On the second line - echo "Hello world";
On the third line - ?>
But I would like to do it by using only one line of code, that would be something like the below one.
$file = 'people.php';
// The new person to add to the file
$person = "John Smith\n";
// Write the contents to the file,
// using the FILE_APPEND flag to append the content to the end of the file
// and the LOCK_EX flag to prevent anyone else writing to the file at the same time
file_put_contents($file, $person, FILE_APPEND | LOCK_EX);
This should work fine using an heredoc:
file_put_contents('people.php', <<<HEREDOC
<?php
echo "Hello World";
?>
HEREDOC
);
<?php
$File = "new.txt"; //your text file
$handle = fopen($File, 'w');
fwrite($handle, '<?php '."\n".' echo "Hello World "; '."\n".'?>');
?>
Your string should be like this
$str = "<?php
echo \"Hello World\";
?>";
then write it to any fileName.php, php while parsing ignores new lines, only semicolons matters.
Edit 1
Since you want to write your string as code, after following above step you will have all your code in a single line more like a compressed code, which will be not human readable, for making it human friendly, you will have to add formatting, a minimal approach for formatting will be to at least add new line chars and tabs for indentation.
for this you will have to do two things, identify chars (a) where you need to add new line feed and those chars (b) where indentation is required. Now before writing to file do some preprocessing add new line chars for char type (a) and at the same time maintain a stack for adding proper number of tabs for indentation.
Try using the following between each line of code to put it on a new line in the php file:
. PHP_EOL .
You can:
<?php
$yourContent = <<<PHP
<?php
echo "Hello world";
echo "This in an example";
$foo = 2;
$bar = 4;
echo "Foo + Bar = ".($foo+$bar);
?>
PHP;
file_put_contents("yourFile.php", $yourContent);
?>
Try following :
<?php
$file = "helloworld.php";
$content = file_get_contents($file);
print_r(htmlspecialchars($content));
?>
Helloworld.php :
<?php
echo "Hello World";
?>
It'll work fine.
It sounds like you need to add a line break to the end of each line. You can use a regular expression search/replace as follows (where $newData already contains the string you want to append to the file):
<?php
$newData = preg_replace('/$/',"\n", $newData);
file_put_contents($file, $newData, FILE_APPEND | LOCK_EX);
?>
This finds the end of each line (indicated by $ in regex) and adds the new line (\n) at that position.
this code is working on my pc. you will also like it.
<?php
$file = 'people.php';
// The new person to add to the file
$person = "<?php
echo 'Hello World';
?>\n";
// Write the contents to the file,
// using the FILE_APPEND flag to append the content to the end of the file
// and the LOCK_EX flag to prevent anyone else writing to the file at the same time
file_put_contents($file, $person, FILE_APPEND | LOCK_EX);
echo $person;
?>

Php line output

I have a file named file.txt that looks like this:
Line 1
Line 2
Line 3
Line 4
using the command:
$content = file_get_contents(file.txt);
echo $content
I get the output on one line:
Line 1 Line 2 Line 3 Line 4
How can I get the output printed over 4 lines?
You actually print it to 4 lines but you can't see in it your browser because of parsing as html.
Use nl2br() to add <br/>'s
$content = file_get_contents(file.txt);
echo nl2br($content);
Also you may send headers that will say that it is not html:
header('Content-type: text/plain');
$content = file_get_contents(file.txt);
echo $content;
<?php echo nl2br($content); ?>
Replaces all regular line breaks ("\n") with "< br >" tags.
If you just want to show a plain text file you should change the content type to text/plain
<?php header("Content-type: text/plain"); ?>
Then all linebreaks will be there as they should in a document.

PHP: uploading text file to page, which shows new line for each text entry, and sorted alphabetically

I'm using PHP and I want to upload a text file, with the output / view showing each line from the text file on a new line (so exactly as it is displayed in the text file).
However, I also want the text file to be sorted alphabetically.
I have working code that uploads the file on each new line, and sorts by upper case, then lowercase - using the sort function
And I have code which sorts it alphabetically (regardless of case), but unfortunately the lines are grouped together, and not separated as I want them to be. - using the natcasesort
I've tried numerous things but not getting anywhere, so hoping someone can help either put the two together, or let me know what I need to do to either piece of code which will make each line show on a new line.
1st CODE NEEDS TO BE SORTED ALPHABETICALLY REGARDLESS OF UPPER/LOWERCASE
2nd CODE NEEDS TO SHOW THE LINE BREAKS
<?php
$file = file("users.txt");
sort($file);
for($i=0; $i<count($file); $i++)
{
$states = explode(",", $file[$i]);
echo $states[0], $states[1],"<br />";
}
?>
<?php
$filename="users.txt";
$lines = array();
$file = fopen($filename, "r");
while(!feof($file)) {
$lines[] = fgets($file,4096);
}
natcasesort($lines);
print_r($lines);
fclose ($file);
?>
You must use
$text = implode("<br />", $lines);
echo $text
It would glue the array and pasting a <br /> in every new line. Of course, if it is being printed in HTML, if not use the carrier \n instead.
And your code would look like:
.
.
.
while(!feof($file)) {
$lines[] = fgets($file,4096);
}
natcasesort($lines);
$text = implode("<br />", $lines);
print_r($text)
.
.
.

Categories