PHP include text file and format

霸气de小男生 提交于 2019-12-11 17:42:35

问题


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.


回答1:


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>



回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/45360673/php-include-text-file-and-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!