PHP Miscellaneous
- Display source code of the website with PHP
- Get the contents of a directory in PHP
- Setting Cookies in PHP
- How to pass values between the pages in PHP?
- How to redirect to another page in PHP?
- Getting current date & time in PHP
- print (get) file extension in PHP
- Include a Class from another file in PHP
- Include php files when they\'re in different folders (directories)
- Import a CSS file in php without using HTML format
- Find similarity of two strings in PHP
- Generate random numbers in PHP
- Find remote IP address in PHP
- PHP program to validate an email address
- How to detect search engine bots with PHP?
- PHP program to calculate the md5 hash from plaintext
- PHP program to convert a string into base64
- PHP program to convert base64 into the simple string
- PHP program to send a text e-mail message
- PHP program to send an HTML email message
- PHP program to download the text file from the specified URL
- PHP program to demonstrate the use of $GLOBALS to access global variables
- PHP program to print the filename of the current executing PHP script
- PHP program to print the version of CGI used by the webserver
- PHP program to print the IP address of a hosted web server
- PHP program to print the server name or website name
- PHP program to print the version of the server software
- PHP program to print the server protocol
- PHP program to print the request method used to access the webpage
- PHP program to print the timestamp of the start of the request
- PHP program to demonstrate the use of $_REQUEST superglobal variable
- PHP program to demonstrate the use of $_POST superglobal variable
Following is the code that will be explained after the program,
Code
Firstly, we have the $lines variable which will keep the result of the source code generated by the file() built-in method of PHP. The file method simply takes a URL and generates the source code file of it. This is then kept as an array line by line and stored inside the lines variable which we can use to display the source code line by line.
To do that, we need to run a loop on this array and echo a line number along with one line. This can be achieved using the forEach loop which takes each element of an array $lines as $line_num associated with $line. We then echo out the line number in bold using the HTML tags in between since this page is sent by server and finally rendered on the browser. We use the htmlspecialchars() in order to safely display the HTML tags.
This program helps us to show the source code of an URL line by line to the user? If you like the article, please share your thoughts in the comments below.
need an explanation for this answer? contact us directly to get an explanation for this answer