In PHP, highlight_file() and file_get_contents() are two commonly used functions to read and display file contents. Each has its advantages, and when used together, they can easily display the PHP source code while preserving syntax highlighting and providing flexibility in controlling the displayed content.
highlight_file() is a built-in PHP function specifically designed to display the source code of a given file with syntax highlighting. Its syntax is as follows:
highlight_file(string $filename, bool $return = false): mixed
$filename: The path of the file to display.
$return: Whether to return the highlighted source code as a string. If set to false (default), the content is directly output; if set to true, it returns a string.
Example:
highlight_file('example.php');
This will directly output the highlighted source code of the example.php file.
file_get_contents() is used to read the contents of a file and return its complete text. It does not provide syntax highlighting, but the content can be processed further, such as filtering or replacing.
Syntax:
file_get_contents(string $filename, bool $use_include_path = false, resource $context = null, int $offset = 0, int $length = null): string|false
Simple example:
$content = file_get_contents('example.php');
echo htmlspecialchars($content);
Here, htmlspecialchars() is used to escape special characters, preventing errors when parsing the HTML source code.
highlight_file() can quickly highlight the entire file, but it does not allow easy filtering or truncating of the content.
file_get_contents() can flexibly read and process content, but it does not provide syntax highlighting.
By combining both, we can first use file_get_contents() to read the file content and then use highlight_string() (the string version of highlight_file()) to highlight the content, providing more flexibility in displaying the content.
Suppose we want to read and highlight the content of a PHP file, while replacing certain URLs in the file with the domain m66.net. Here’s how we can do it:
<?php
// Read file content
$filename = 'test.php';
$content = file_get_contents($filename);
<p>// Replace all URLs with domain m66.net<br>
// This regex matches http(s):// followed by domain, replacing with http(s)://m66.net<br>
$pattern = '/(https?://)([^/\s]+)/i';<br>
$replacement = '$1m66.net';<br>
$content = preg_replace($pattern, $replacement, $content);</p>
<p>// Highlight the replaced content<br>
highlight_string($content);<br>
?><br>
Use file_get_contents() to retrieve the file content.
Use a regular expression to replace the URL domain part with m66.net.
Use the highlight_string() function to display the highlighted content.
This ensures that all URLs in the displayed code are replaced with m66.net, making it easier to handle and demonstrate consistently.
By combining highlight_file() or highlight_string() with file_get_contents(), you can flexibly control how PHP source code is displayed. This approach is especially useful when dynamic content processing (like replacing URLs) is needed, as it allows you to first retrieve the content and then highlight it for better presentation.
Related Tags:
file_get_contents