note 128223 added to function.highlight-string

From: Date: Wed, 15 Feb 2023 12:47:49 +0000
Subject: note 128223 added to function.highlight-string
Groups: php.notes 
Request: Send a blank email to php-notes+get-220179@lists.php.net to get a copy of this message
Hi Folks! I wrote a function to integrate PHP source code into a HTML5 page using modern CSS design for additional line numbering. Try it and use it for free. Cheers! – Ulrich <?php function PHPSourceBox($ext_PHPSourceFile) { // global settings $sourceLines = 0; $sourceWidth = 0; $oneChar = 0.48637; // the width 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); user-select: none; -ms-user-select: none; -moz-user-select: none; -webkit-user-select: none; } #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); } ?> ---- Server IP: 45.112.84.4 Probable Submitter: 80.90.13.130 (proxied: 2003:da:cf1c:bf00:9c71:a484:3644:261f) ---- Manual Page -- https://php.net/manual/en/function.highlight-string.php Edit -- https://main.php.net/note/edit/128223 Del: integrated -- https://main.php.net/note/delete/128223/integrated Del: useless -- https://main.php.net/note/delete/128223/useless Del: bad code -- https://main.php.net/note/delete/128223/bad+code Del: spam -- https://main.php.net/note/delete/128223/spam Del: non-english -- https://main.php.net/note/delete/128223/non-english Del: in docs -- https://main.php.net/note/delete/128223/in+docs Del: other reasons-- https://main.php.net/note/delete/128223 Reject -- https://main.php.net/note/reject/128223 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#220179) next »