Current Location: Home> Latest Articles> How to embed highlight code from highlight_file output in HTML page

How to embed highlight code from highlight_file output in HTML page

gitbox 2025-05-28

When developers need to display PHP source code on web pages, the built-in highlight_file() function of PHP is a very convenient method. It can output the code of PHP files directly in syntax highlighting, which is ideal for document presentations, tutorial websites, or code sample demonstrations.

This article will explain in detail how to embed the output of the highlight_file() function in an HTML page and maintain a good page structure and style.

1. Introduction to highlight_file() function

highlight_file() is a function provided by PHP to display the contents of a PHP file in HTML format with syntax highlighting. The basic usage is as follows:

 highlight_file('example.php');

This code will highlight the PHP code in the example.php file and output it to the browser.

2. Basic methods to embed into HTML pages

If you want to embed highlighting code into a complete HTML page, you can use the following structure:

 <!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>PHP Highlight Example</title>
    <style>
        body {
            font-family: monospace;
            background-color: #f7f7f7;
            padding: 20px;
        }
        code {
            display: block;
            background-color: #ffffff;
            border: 1px solid #ccc;
            padding: 10px;
            overflow-x: auto;
        }
    </style>
</head>
<body>
    <h1>Code highlight output</h1>
    <code>
        <?php highlight_file('https://gitbox.net/demo.php'); ?>
    </code>
</body>
</html>

Please note:

  • highlight_file() outputs content that already contains HTML tags, so although it is feasible to place it directly in the <code> tag, the style may be overwritten or duplicated.

  • The more recommended way is to not use the extra <code> tag, but output it directly to the page, as follows:

 <!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>PHP Highlight</title>
    <style>
        body {
            font-family: monospace;
            background-color: #f0f0f0;
            padding: 20px;
        }
    </style>
</head>
<body>
    <h2>PHP Source code highlighted</h2>
    <?php highlight_file('https://gitbox.net/demo.php'); ?>
</body>
</html>

In this way, the highlight_file() function directly outputs the highlighted HTML to the page, so no additional tag wrapping is required.

3. Use output buffer to further control the code output

If you want to further process the highlighted HTML, such as adding your own style wrapper, you can use output buffering:

 <?php
ob_start();
highlight_file('https://gitbox.net/demo.php');
$highlightedCode = ob_get_clean();
?>

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>With style PHP Highlight</title>
    <style>
        .code-box {
            background-color: #282c34;
            color: #abb2bf;
            padding: 20px;
            border-radius: 5px;
            font-family: Consolas, monospace;
            overflow-x: auto;
        }
        pre {
            margin: 0;
        }
    </style>
</head>
<body>
    <div class="code-box">
        <?php echo $highlightedCode; ?>
    </div>
</body>
</html>

This method gives you complete control over how the highlighted code is wrapped, suitable for scenarios where custom styles are required.

4. Things to note

  • highlight_file() can only highlight local or allowed remote PHP files. Some servers may disable remote file access for security reasons (such as allow_url_fopen = Off ).

  • The file before output must exist, otherwise an error will be reported or an empty content will be output.

  • Avoid using this function on PHP files containing sensitive information to prevent information leakage.

Summarize

Using highlight_file() makes it very convenient to embed PHP highlighting code in HTML pages. Whether it is teaching, document writing, or development demonstration, it takes only a few lines of code to quickly realize the aesthetic presentation of the code. If you have more complex presentation needs, you can also combine output buffering and CSS styles to create more advanced code presentation components.