How to get the source code of a web page using PHP ? Last Updated : 26 Jul, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a webpage, for which we need to find its source code using PHP. For this, we are going to use the PHP htmlspecialchars() function which converts any predefined characters to their subsequent HTML entities.Example 1: Suppose we take a sample website that looks like the below image, let us see what output would the code produce for the same.HTML code: HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>SAMPLE WEBSITE</title> </head> <body style="color: green;"> <h1>This is a Sample website</h1> <p> In this article we will learn how to extract a web page's source code through PHP </p> </body> </html> Output:Output as source code contents:Now let us suppose the above website is hosted on a localhost, the code would fail to load the source code in such a case. The output produced would be something similar to this.Output:Warning: file(file:///D:/Html%20website/gfg%20sample%20website/index.html): Failed to open stream: No such file or directory in C:\xampp\htdocs\programs\source code.php on line 2 Warning: foreach() argument must be of type array|object, bool given in C:\xampp\htdocs\programs\source code.php on line 3Example 2:Approach:Store its elements into an array.Traverse the array using a PHP programming loop.Convert and print each character into its subsequent HTML entity.PHP code: Below is the implementation of the above approach. PHP <?php // Storing the elements of the webpage into an array $source_code = file('https://www.geeksforgeeks.org'); // 1. traversing through each element of the array // 2.printing their subsequent HTML entities foreach ($source_code as $line_number => $last_line) { echo nl2br(htmlspecialchars($last_line) . "\n"); } ?> Output: Comment More infoAdvertise with us Next Article Convert HTML source code to JSON Object using Python D debjani1413 Follow Improve Article Tags : Web Technologies PHP PHP-Questions Similar Reads How to get Source Code of any website ? A Website is defined as a collection of web pages. A website can be designed using HTML, CSS, Bootstrap, etc. HTML language decides what will appear on the webpage, and CSS language depicts how it will appear. Suppose we see a website eg, geeksforgeeks.org, and we want its source code, then there ar 1 min read Convert HTML source code to JSON Object using Python In this post, we will see how we can convert an HTML source code into a JSON object. JSON objects can be easily transferred, and they are supported by most of the modern programming languages. We can read JSON from Javascript and parse it as a Javascript object easily. Javascript can be used to make 3 min read How to get form data using POST method in PHP ? PHP provides a way to read raw POST data of an HTML Form using php:// which is used for accessing PHPâs input and output streams. In this article, we will use the mentioned way in three different ways. We will use php://input, which is a read-only PHP stream. We will create a basic HTML form page wh 2 min read How to parse and process HTML/XML using PHP ? In this article, we will learn how to process XML using PHP. We have already learnt the basics of XML and their differences with respect to HTML. Different elements of XML can be learnt from XML Elements to understand the working of the following programs. The simplexml_load_string() function is use 2 min read How to get the Base URL with PHP ? The term "Base URL" refers to the base or starting point for a set of related URLs. For example, if you have a website with multiple pages or an API with various endpoints, the Base URL is the common part shared by all those URLs. It typically includes the protocol (such as "http" or "https"), the d 2 min read How to embed PHP code in an HTML page ? PHP is the abbreviation of Hypertext Preprocessor and earlier it was abbreviated as Personal Home Page. We can use PHP in HTML code by simply adding a PHP tag without doing any extra work. Example 1: First open the file in any editor and then write HTML code according to requirement. If we have to a 2 min read Like