All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Read HTML From Text File Using PHP

Last Updated : Jul 1, 2023

IN - PHP HTML | Written & Updated By - Dikshita

In this tutorial we will show you how to read HTML content from a text file using PHP. In this we read a text file which is present in our folder and have HTML contents by using this method it will also display inline styling.You may also like read xml file using PHP.

Read HTML From Text File Using PHP

To Read HTML From Text File It Takes Only Two Steps:-

  1. Make a Text file and add some HTML code for reading
  2. Make a PHP file to read HTML content from a text file

Step 1. Make a Text file and add some HTML code for reading

We make a Text file and save it with a name content.text

<h1>Sample Text File</h1>
<p>This is used by our PHP file to read HTML content</p>
<br>
<table align=center>
<tr>
<td>Suresh</td><td>PHP Developer</td>
</tr>
<tr>
<td>Pooja</td><td>Web Designer</td>
</tr>
<tr>
<td>Amit</td><td>HTML Developer</td>
</tr>
<tr>
<td>Ram</td><td>Data Analyst</td>
</tr>
</table>

In this step we create a text file and write some HTML content for reading.You can also add inline styling to your HTML code to display content in style. You may also like read and write csv file using PHP.

Step 2. Make a PHP file to read HTML content from a text file

We make a PHP file and save it with a name read_html.php

<html>
<body>
<div id="wrapper">

<?php
 $myfile = fopen("content.txt", "r") or die("Unable to open file!");
 echo fread($myfile,filesize("content.txt"));
 fclose($myfile);
?>

</div>
</body>
</html>

In this step we use PHP fopen function to open our 'content.txt' file in read mode and then use fread() function to display file content. You may also like read and delete file from folder using PHP.

That's all, this is how to read HTML content from text file using PHP. You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

I hope this tutorial on read HTML from text file helps you and the steps and method mentioned above are easy to follow and implement.