showPHPSource 2 – der Quellcode



















































<?php
function PHPSourceBox($ext_PHPSourceFile) {

    // global settings
    $sourceLines = 0;
    $sourceWidth = 0;
    $oneChar = 0.50900; // the width (unit is em) of one character of the standard sourcecode font (may change if you use a different typeset).

    // Get the source file twice, once for highlighting, once for counting the nuber and max-length of lines.
    $the_HighFile = highlight_file($ext_PHPSourceFile, $return = true);
    $the_SourceFileArray = preg_split("/\R/", htmlspecialchars(file_get_contents($ext_PHPSourceFile)));

    // Calculate the number of sourcecode lines and generate them.
    if ($sourceLines == 0) $sourceLines = count($the_SourceFileArray);
    for ($i = 1; $i <= $sourceLines; $i++) { $the_lineNumbersHTML .= '<span><br></span>'; }

    // Calculate the sourcecode box width depending on the num of chars an transform to string adding "em" behind.
    foreach ($the_SourceFileArray as $value) {
        $value = preg_replace ('/\s{3,}/', '', html_entity_decode($value));
        if ($sourceWidth < strlen($value)) $sourceWidth = strlen($value);
    }
    $the_CodeBoxWidth = (ceil($sourceWidth * $oneChar * 10)/10)."em";

    // Sourcecode-box specific CSS - styles.
    $HTML_SourceCodeStyles = <<<EOT
        section { border-block: thin solid black; border-inline: 2px solid grey; padding: 0.3em; overflow-y: scroll; overflow-block: scroll; height: calc(100vh - 8em); }
        #codecontent { margin-left: 2em; width: $the_CodeBoxWidth; }
        #linenumbers { counter-reset: mycounter 0; float: left; text-align: right; width: 1.5em; background-color: whitesmoke; border-right: 0.5pt solid; }
        #linenumbers code span::before { content: counter(mycounter); counter-increment: mycounter; }
EOT;

    // Combine the sourcecode-box text.
    $HTML_SourceCodeBox = <<<EOT
    <section>
        <div id="linenumbers">
            <code>
                $the_lineNumbersHTML
            </code>
        </div>
        <div id="codecontent">
            $the_HighFile
        </div>
    </section>
EOT;
    
    //return the specific styles ond the sourcecode-box.
    return array ('HTML_SourceCodeStyles' => $HTML_SourceCodeStyles, 'HTML_SourceCodeBox' => $HTML_SourceCodeBox);
}
?>