Join TalkersCode Now !

Get Latest Tutorials And Links On Web Development
And Get Existing Offers Via Email

Read HTML From Text File Using PHP

Tuesday, Jan 24, 2017

Tags:- PHP HTML


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.

Join Us With Our 21000+ Subscribers And Get Our Latest Tutorials Update Via Email

Are You Starting A New Blog
Do You Want Best Web Hosting?

Try World's Best Web Hosting Provider BlueHost
At Just $3.95/mo

Hurry Up! Limited Time Offer

Yes, I Want To Check Out BlueHost

Check The World's Best SEO And All-In-One Marketing Toolkit
For Digital Marketing Professionals SEMRUSH

Check Out SEMRUSH