DHamrick
DHamrick

Reputation: 8488

Display c++ code in php

I am trying to display the contents of a .cpp file in php. I am loading it using fread when I print it out it comes out formatted incorrectly. How can I keep the format without escaping each character?

Upvotes: 0

Views: 668

Answers (3)

noel aye
noel aye

Reputation: 351

<?php

echo "<pre><code>";
$filename = "./test.cpp";
$handle = fopen($filename, "r");

if ($handle) {
    while (!feof($handle)) {
        $buffer = fgets($handle, 4096); // assuming max line len is 4096.
        echo htmlspecialchars($buffer);
    }
    fclose($handle);
}
echo "</code></pre>";

?>

We need htmlspecialchars function to print it out correctly.

Upvotes: 1

deceze
deceze

Reputation: 522597

Assuming you want to look at it in a web browser:

<pre>
    <code>
        <?php echo htmlspecialchars(file_get_contents($file)); ?>
    </code>
</pre>

Upvotes: 4

user36457
user36457

Reputation:

print it out between the HTML <pre> & <code> tags.

Upvotes: 2

Related Questions