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.
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.
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 3
- 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.