Display c++ code in php - php

I am trying to display the contents of a .cpp file in php. I am loading it using fread when I print it out it comes out formatted incorrectly. How can I keep the format without escaping each character?

Assuming you want to look at it in a web browser:
<pre>
<code>
<?php echo htmlspecialchars(file_get_contents($file)); ?>
</code>
</pre>

print it out between the HTML <pre> & <code> tags.

<?php
echo "<pre><code>";
$filename = "./test.cpp";
$handle = fopen($filename, "r");
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle, 4096); // assuming max line len is 4096.
echo htmlspecialchars($buffer);
}
fclose($handle);
}
echo "</code></pre>";
?>
We need htmlspecialchars function to print it out correctly.

Related

PHP - echo and fgets weird characters

I'm trying to display the content of a text file on my website using PHP's fgets, but when I echo the lines in combination with something else (<br>, \n, ...) I get pretty weird characters.
Here's my code :
<?php
header('Content-Type: text/plain;charset=utf-8');
$handle = #fopen("test.txt", "r");
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
echo $buffer."<br>";
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
?>
Here is the content of test.txt :
1
2
3
4
5
... (6 - 18)
19
20
And here's what I get :
Result with <br>
If I use \n instead of <br>, I don't even get Chinese characters :
Result with \n
I think the issue comes from fgets(), because when I print only one line (without the loop) I get the same issue, but if replace $buffer by "1" (echo "1"."<br>";) I get the expected result.
EDIT
As suggested I modified the code to add header('Content-Type: text/plain;charset=utf-8'); at the beginning of the php file, and modified the output as well.
I found that the issue must be somewhere in the text file : I created a new one and the issue was gone.
I don't know the original encryption of the file because a friend gave it to me.
I'll update this answer if I find out exactly what was going on.
EDIT
I made a copy via TextEdit and when saving it the default encoding format was UTF-16, I guess that was the problem.
Working DEMO: http://phpfiddle.org/main/code/xrsk-a0uv
Text File:: http://m.uploadedit.com/ba3s/1500405331493.txt
Problem: at the Time of create text file it's select the encoding format is UTF-16. !! UTF-8 by default for nodepad,nodepad++,sublime etc.. !!
<?php
header('Content-Type: text/plain;charset=utf-8');
$handle = #fopen("http://m.uploadedit.com/ba3s/1500405331493.txt", "r");
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
echo $buffer."</br>";
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
?>
NOTE: Add header for charset-utf-8
header('Content-Type: text/plain;charset=utf-8');
OUTPUT Using With "\n"
OUTPUT Using With "</br>"

PHP: How to Read Text File like Notepad++

I have a text file that contains lines of text:
When I open it in Notepad ++ It is aligned and the columns are separated with space:
Is there any way to echo this through my PHP code to give an output like Notepad++?
I've used the following code
<?php
header('Content-Type: text/html; charset=UTF-8');
echo file_get_contents("MAGFLE_Greenbnk_Caraga.txt");
?>
Output:
Do I need to declare ANSII/UTF8 encoding/decoding in my code?
Your help will be very much appreciated
$handle = #fopen("myfile.txt", "r");
if($handle)
{
while ( ($buffer = fgets($handle, 4096) ) !== false )
{
echo $buffer."\r\n";
}
#fclose($handle);
}

How can I get php to display the entire content of my file?

I have a text file :
Topic identified: Sports
I have 2 php files:
file 1:
<?php
include "topic_detection.php";
$text = isset($_POST["text"]) ? $_POST["text"] : '';
$file = "textfile.txt";
$outputfile= "outputfile.txt";
if(!empty($_POST)){
writetofile($file, $text, "w");
execpython($file);
$topic = getoutput($outputfile);
}
?>
file 2: topic_detection.php
<?php
function writetofile($file, $content, $writeType){
$fo = fopen($file, $writeType);
if($fo){
fwrite($fo,$content);
fclose($fo);
}
}
function execpython($file){
system("python predict.py $file");
}
function getoutput($file1){
$fh= fopen($file1, 'r');
$theData = fread($fh,1);
fclose($fh);
echo $theData;
return $theData;
}
?>
On trying to get the output of $theData, the only output I receive is a 'T' I am guessing this T is coming from the first letter in the text file. In that case, am I not making the call correctly?
here is my call:
<div align="center"><h4 style="line-height:150%;"><?php echo $topic; ?></h5></div>
You are passing fread a length of 1:
$theData = fread($fh,1);
You will always get only the first character.
To read the entire file: (From the PHP docs http://php.net/manual/en/function.fread.php)
$contents = fread($handle, filesize($filename));
You could also use http://php.net/manual/en/function.file-get-contents.php

The result of "echo fgets()" in a txt file is not the same

I am trying to include in my website a txt file with php using the following code
<?php
$myFile = '[http://users.otenet.gr/~vag1976/optonio/NOAAMO.TXT]';
$fp = fopen("$myFile", "r");
while(!feof($fp)) {
echo fgets($fp) . "<br />";
}
fclose($fp);
?>
The problem is that using this php code the result is not exactly the same as the original. The gaps (spaces) between the words are lost and are replaced by only one space. The viewing result is terrible. Any ideas?
That's not a PHP problem, it's just the browser which does not render multiple 'normal' spaces (if you don't use non-breaking-spaces or the special <pre> tag).
You should also use a real line feed instead of <br /> tags:
<?php
$myFile = '[http://users.otenet.gr/~vag1976/optonio/NOAAMO.TXT]';
// You don't need to use "$myFile" here, just use the variable without quotes
$fp = fopen($myFile, "r");
echo "<pre>";
while(!feof($fp)) {
echo fgets($fp) . "\n";
}
echo "</pre>";
fclose($fp);
?>
If your file is not too large, you can also use file_get_contents():
<?php
$myFile = '[http://users.otenet.gr/~vag1976/optonio/NOAAMO.TXT]';
echo '<pre>' . file_get_contents($myFile) . '</pre>';
?>
You could write around it some <pre>-tags:
<pre><?php /* your code */ ?></pre>
<pre> preserves whitespaces.
A browser always replaces multiple following whitespaces by one single.
Then you can also use \n instead of <br /> for doing your line wraps.
To keep the spaces use <pre></pre> tags.
You'll need to use the <pre> tag. It preserves excess whitespace.
<?php
$myFile = '[http://users.otenet.gr/~vag1976/optonio/NOAAMO.TXT]';
$fp = fopen("$myFile", "r");
echo "<pre>";
while(!feof($fp)) {
echo fgets($fp) . "<br />";
}
echo "</pre>";
fclose($fp);
?>
Also, I hope you realize <br /> isn't HTML5 semantics-enriched linguistics.
It would appear that you need to enclose whatever you return with fgets() in a <pre> tag to preserve the whitespace.
Original answer: https://stackoverflow.com/a/2805900/2305229
Modified code:
<?php
$myFile = '[http://users.otenet.gr/~vag1976/optonio/NOAAMO.TXT]';
$fp = fopen("$myFile", "r");
while(!feof($fp)) {
echo "<pre>";
echo fgets($fp) . "<br />";
echo "</pre>"
}
fclose($fp);
?>

File I/O to textarea in PHP

My friend and I have a little spare time home page together. He's not a programmer, and in order for him to be able to change some text on the front page, I created a php-script that
1) Reads data from file "tester.txt" (this is the text that should go on the front page)
2) Prints this text to a textarea, where you can edit the text and submit it again
3) Writes the edited text to the same file, "tester.txt"
The two functions Read(); and Write(); look like this
function Read() {
$file = "tester.txt";
$fp = fopen($file, "r");
while(!feof($fp)) {
$data = fgets($fp, filesize($file));
echo "$data <br>";
}
fclose($fp);
}
function Write() {
$file = "tester.txt";
$fp = fopen($file, "w");
$data = $_POST["tekst"];
fwrite($fp, $data);
fclose($fp);
}
The only problem I have is that when the text is printed to a text area the line returns are written as <br> - and I don't really want it to do that, because when you edit some of the code and rewrites it, another layer of <br>'s appear. Here's a screenshot to illustrate:
Is there any workaround to this?
Thanks!
If you need the rest of the code, here it is:
<html>
<head>
<title>Updater</title>
</head>
<body>
<?php
function Read() {
$file = "tester.txt";
$fp = fopen($file, "r");
while(!feof($fp)) {
$data = fgets($fp, filesize($file));
echo "$data <br>";
}
fclose($fp);
}
function Write() {
$file = "tester.txt";
$fp = fopen($file, "w");
$data = $_POST["tekst"];
fwrite($fp, $data);
fclose($fp);
}
?>
<?php
if ($_POST["submit_check"]){
Write();
};
?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<textarea width="400px" height="400px" name="tekst"><?php Read(); ?></textarea><br>
<input type="submit" name="submit" value="Update text">
<input type="hidden" name="submit_check" value="1">
</form>
<?php
if ($_POST["submit_check"]){
echo 'Text updated';
};
?>
</body>
</html>
This is simpler than you think. You shouldn't be outputting the <br> tags as the textarea already contains the entered newline characters (\r\n or \n). You don't have to read the file like that, if you read it this way, you never have to worry about the character contents.
Change:
$fp = fopen($file, "r");
while(!feof($fp)) {
$data = fgets($fp, filesize($file));
echo "$data <br>";
}
fclose($fp);
to:
echo file_get_contents( $file);
Problem solved.
This is happening because while writing the contents to the text area, you're putting a <br> at the end of each line. But in a textarea line breaks are noted by "\n". When you're saving your existing text, the next time the line breaks are replaced with more <br>.
While printing out the content on a public page, keep the . But in the editing page, remove the br.
Here's what I would have done with the PHP code:
<?php
define("FILE_NAME", "tester.txt");
function Read()
{
echo #file_get_contents(FILE_NAME);
}
;
function Write()
{
$data = $_POST["tekst"];
#file_put_contents(FILE_NAME, $data);
}
?>
<?php
if ($_POST["submit_check"])
{
Write();
}
?>
You use echo "$data <br>"; - just make that echo $data; ?
At a guess, this is hosted on a *nix machine, and he is using a Windows machine to do the editing?
If this is the case, changing your write function to this should solve the problem:
function Write(){
$file = "tester.txt";
$fp = fopen($file, "w");
$data = str_replace(array("\r\n","\r"),"\n",$_POST["tekst"]);
fwrite($fp, $data);
fclose($fp);
};
You don't need the <br>'s. Depending how your <textarea> is configured, by default lines are hard wrapped using \n's. These are preserved when you save the file, therefore you don't need to add your own line breaks.
Use regex to replace the break tag with a newline character
preg_replace('#<br\s*/?>#i', "\n", $data);
You can find a more detailed explanation answered here

Categories