I have a simple text file. The file name is kpop.txt - What I want to do is include the contents of the file in a webpage. I am using php for includes (header / footer stuff).
I would like the contents of the kpop.txt file to remain formatted similar to it's current form. The only real difference that I would like to make is to increase the font size.
I am currently using
<?php
include("kpop.txt");
?>
I have also tried
<pre>
<font size ="12">
<?php
include("kpop.txt");
?>
</font>
</pre>
What I want to see is my text like this but simply with a larger font.
FTUS43 KGGW 271140
TAF
KP01 271140Z 2706/2806 08010KT P6SM SCT140
FM271500 12011KT P6SM SCT110
FM271900 14011KT P6SM BKN120
FM280000 14008KT P6SM VCSH BKN100
FM280300 13006KT P6SM VCSH SCT100
AMD NOT SKED. UNFL=
TAF
KM75 271140Z 2706/2806 07008KT P6SM SCT110
FM271500 11008KT P6SM FEW150
AMD NOT SKED. UNFL=
Another solution that did not work is
<?php
myfilename = "kpop.txt";
$TAF = file_get_contents(myfilename);
$TAFlines = explode("\n", $TAF);
//echo $TAF;
echo $TAFlines;
}
?>
I have also tried using the file_get_contents function along with an explode function but cannot seem to get that to work properly. Any help is greatly appreciated.
So this is what I came up with.
<pre>
<?php
//Read in the file and increase the font 200%
$TAF = file_get_contents("kpop.txt");
echo "<div style='font-size:200%'><p>$TAF</p></div>";
?>
</pre>
It may not be pretty but it works. Thank you all for your help.
I would suggest you use CSS, rather than the deprecated <font> tag:
<pre style="font-size:120%">
<?php echo file_get_contents('kpop.txt') ?>
</pre>
It's pretty simple, use fopen() and then read it fread(),just use echo to display it:
<?php
$myfile = fopen("kpop.txt", "r") or die("Unable to open file!");
echo '<span style="font-size:30px;">' . fread($myfile,filesize("kpop.txt"))
. ' </span>';
fclose($myfile);
?>
More details: https://www.w3schools.com/php/php_file_open.asp
Related
I am trying to make "manner friendly" website. We use different declination dependent on gender and other factors. For example:
You did = robili
It did = robilo
She did = robila
Linguisticaly this is very simplified (and unlucky) example! I would like to change html text in php file where appropriate. For example
<? php
something
?>
html text of the page and somewhere is the word "robil"
<div>we tried to robil^i|o|a^</div>
<? php something ?>
Now I would like to replace all occurences of different tokens ^characters|characters|characters^ and replace them by one of their internal values according to "gender".
It is easy in javascript on the client side, but you will see all this weird "tokenizing" before javascript replace it.
Here I do not know the elegant solution.
Or do you have better idea?
Thanks for advice.
You can add these scripts before and after the HTML:
<?php
// start output buffering
ob_start();
?>
<html>
<body>
html text of the page and somewhere is the word "robil"
<div>we tried to robil^i|o|a^, but also vital^si|sa|ste^, borko^mal|mala|malo^ </div>
</body>
</html>
<?php
$use = 1; // indicate which declination to use (0,1 or 2)
// get buffered html
$html = ob_get_contents();
ob_end_clean();
// match anything between '^' than's not a control chr or '^', min 5 and max 20 chrs.
if (preg_match_all('/\^[^[:cntrl:]\^]{3,20}\^/',$html,$matches))
{
// replace all
foreach (array_unique($matches[0]) as $match)
{
$choices = explode('|',trim($match,'^'));
$html = str_replace($match,$choices[$use],$html);
}
}
echo $html;
This returns:
html text of the page and somewhere is the word "robil" we tried to
robilo, but also vitalsa, borkomala
I am trying to get the below code to work, so that I understand what I am doing on a larger project. For the most part,I'm just trying to make sure I can grab elements by their tag. The code seems to break at "$html = new simple_html_dom();" because if I comment that out, then I get the two print outs. but if it's not commented out, nothing shows up on the screen at all.
<?php
# create and load the HTML
include('simple_html_dom.php');
print "hello ";
$html = new simple_html_dom();
print "world"
#$html->load("<html><body><p>Hello World!</p><p>We're here</p></body></html>");
# get an element representing the second paragraph
#$element = $html->find("p");
# modify it
#$element[1]->innertext .= " and we're here to stay.";
# output it!
#echo $html->save();
?>
Is the simple_html_dom.php file actually exiting and loaded? You might want to write that include similar to:
<?php
if(!#file_exists('./simple_html_dom.php') )
{
echo 'can not include: simple_html_dom.php';
die();
}
else
{
include('./simple_html_dom.php');
}
?>
So the PHP parser on the server will let you know if there is a problem.
I have this:
ob_start(); ?>
<div class="" id="loading">
<?php echo file_get_contents($page["texto"]) ?>
</div> <?php
$content = ob_get_clean();
But I would like to have that template html in a separate file, just like:
$content = file_get_contents('cms/template.php');
Now this won't work because it has php tags inside and when retrieving the string it gets as
how can I achieve this without using a dirty hack like:
$pre = file_get_contents('part1');
$var = file_get_contents($page["texto"]);
$post = file_get_contents('part2');
And adding all them...
It's still hack, but unless you use a templating system like Twig you have no choice:
ob_start();
include 'cms/template.php';
$content = ob_get_clean();
echo $content;
ob_start enables output buffering so nothing gets sent to the browser. We then include the file which will execute PHP normally. We then use ob_get_clean to get the contents of the output buffer (which is your template file). and disable the output buffer, discarding it's contents, as we have the contents in $content.
I am saving and editing data in text files through a text area using CKeditor and everything is working smoothly. Everything except new lines ("<br />") that don't show when I try to edit/update the text file via my update.php. I really can't find out what is the issue, I have tried to replace tag after tag and did not manage to solve the problem.
Code for reading and writing on the text file:
$text1 = "../conteudos/start/text1.txt";
if (isset($_POST['body1'])) {
$newData = nl2br($_POST['body1']);
$handle = fopen($text1, "w");
fwrite($handle, $newData);
fclose($handle);
}
// ------------------------------------------------
if (file_exists($text1)) {
$myData1 = file_get_contents($text1);
$myData1 = strip_tags($myData1);
}
Code for editing the text contents:
<textarea class="ckeditor" name="body1" id="body1">
<?php echo str_replace("<br />","",$myData1); ?>
</textarea>
As mentioned before, the text shows up nicely on my index.php with no html tags whatsoever, but when I try to edit it via the text area above I still get no tags, but I get all the text into one single line. This really should be working because I am using "nl2br" function, but apparently something is canceling it.
What can I do?
I think what you are trying to do is:
$text1 = "../conteudos/start/text1.txt";
if (isset($_POST['body1'])) {
$newData = nl2br($_POST['body1']);
$handle = fopen($text1, "w");
fwrite($handle, $newData);
fclose($handle);
}
// ------------------------------------------------
if (file_exists($text1)) {
$myData1 = file_get_contents($text1);
//Change it here first
str_replace("<br />","\n",$myData1); //You also forgot the new line character I think.
$myData1 = strip_tags($myData1);
}
Then you can do this:
<textarea class="ckeditor" name="body1" id="body1">
<?php echo $myData1; ?>
</textarea>
You made a small logic error according to what I see. According to my understanding, you want to strip out the tags but preserve the new line. So change the "< br />" first before you strip out the tags. Hopefully that's what you want I guess.
You are stripping the tags from your file ($myData1 = strip_tags($myData1)). <br /> is a tag, so you're stripping it out too!
This makes your str_replace useless, since the tag has already been stripped. In any case, you shouldn't need that nl2br in the first place, since newline characters are perfectly valid inside text files...
Something very strange happened because according to the user Touch, his method was working on his computer. Unfortunately it wasn't working on mine! So after a while thinking I came to the conclusion that I was over doing some process of replacement of tags. In order to confirm or not this theory of mine I decided do "back-engineer" Touch's method by erasing line by line and seeing what the result was. In the end I saw that my conclusion was correct, I was over doing process of tag replacement because this code:
$text2 = "../conteudos/start/text2.txt";
if (isset($_POST['body2'])) {
$newData = nl2br($_POST['body2']);
$handle = fopen($text2, "w");
fwrite($handle, $newData);
fclose($handle);
}
// ------------------------------------------------
if (file_exists($text2)) {
$myData2 = file_get_contents($text2);
$myData2 = $myData2;
}
worked in perfection. I can only think that this was due to I was using KCEditor...
A big thanks to all that answered, maing me think and helping me this way to achieve my goal!
I'm trying to open a text file and output its contents with the code below. The text file includes line breaks but when I echo the file its unformatted. How do I fix this?
Thanks.
<html>
<head>
</head>
<body>
$fh = fopen("filename.txt", 'r');
$pageText = fread($fh, 25000);
echo $pageText;
</body>
</html>
To convert the plain text line breaks to html line breaks, try this:
$fh = fopen("filename.txt", 'r');
$pageText = fread($fh, 25000);
echo nl2br($pageText);
Note the nl2br function wrapping the text.
One line of code:
echo nl2br( file_get_contents('file.txt') );
If you just want to show the output of the file within the HTML code formatted the same way it is in the text file you can wrap your echo statement with a pair of pre tags:
echo "<pre>" . $pageText . "</pre>;
Some of the other answers look promising depending on what you are trying todo.
For simple reads like this, I'd do something like this:
$fileContent = file_get_contents("filename.txt");
echo str_replace("\n","<br>",$fileContent);
This will take care of carriage return and output the text. Unless I'm writing to a file, I don't use fopen and related functions.
Hope this helps.
Before the echo, be sure to include
header('Content-Type: text/plain');
Are you outputting to HTML or plain text? If HTML try adding a <br> at the end of each line. e.g.
while (!feof($handle)) {
$buffer = fgets($handle, 4096); // Read a line.
echo "$buffer<br/>";
}
Trying to get line breaks to work reading a .txt file on Apache2 and PHP 5.3.3 with MacOSX 10.6.6 and Camino, the echo nl2br( $text); didn't work right until I printed the file size first too.
BTW it doesn't seem to matter if the .txt file has Linux/MacOSX LF or Windows CRLF line breaks or the text encoding is UTF-8 or Windows Latin1, Camino gets it out OK.
<?php
$filename = "/Users/Shared/Copies/refrain.txt";
$file_ptr = fopen ( $filename, "r" );
$file_size = filesize ( $filename );
$text = fread ( $file_ptr, $file_size );
fclose ( $file_ptr );
echo ( "File size : $file_size bytes<br> <br>" );
echo nl2br ( $text );
?>
You need to wrap your PHP code into <?php <YOU CODE HERE >?>, and save it as .php or .php5 (depends on your apache set up).
Say you have an index.php file hosted by the web server. You want to insert some multi-line text file contents into it. That's how you do it:
<body>
<div>Some multi-line message below:</div>
<div><?= nl2br(file_get_contents('message.txt.asc')); ?></div>
</body>
This <?= ... ?> part is just a shorthand, which instructs the web server, that it needs to be treated as a PHP echo argument.