PHP Code for show and coloring code on websites
Copy a codefile like this php 1:1 into a folder and show it like this below
<?php
// remove the echo lines below up to the other remove statement - is just to show the code how the Title, Subtitle and Description are used
// "<!--Title: PHP Code for show and coloring code on websites<br>";
// "<!--Subtitle: Description:<br>";
// "<!-- Copy a codefile like this php 1:1 into a folder and show it like this below<br>";
// "<!--Download:php_listcode.zip<br>";
// "-->";
// remove the echo part above
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
/*
/* Function: This script show code on a website with following options:
/* - read all codefiles from the defined folder (title and description part has to be part of the codefile)
/* - coloring key words in the code (just format found string - no check is it part of a longer word)
/* - coloring comment with inline comment or start and end tag requognizing
/* - coloring words starting with a specifc string (regex)
/* - coloring from a character to a character (regex)
/* - coloring on own regex strings
/* - add titles if a line start with the defined title key
/* - add description if a line start with the defined description key
/* - add any other textline if it is defined by a key
/* - add images if a line start with the defined image key followed by the filename
/* - add a download if a line start with the defined download key followed by the filename
/* - css style are included and can be changed to your wishes
/*
/* The coloring and style can be edited in the parameters below or in the style defnition
/* Path, Index, Scrolloption and coloring typ can be inserted by GET (or use the defaults). This
/* allow you to use this script one time and show code from different subfolders and color types
/* This script can be included as iframe or in another php script. The header of this file is to
/* use otherwise the css style will not work.
/*
/* Version: 20200120
/* Autor: NEWROCK
/*
/* PHP: Requires PHP 8 or higher (php 7 won't work because of the use of str_contains)
/*
/* Examples: VBA one codefile with more different Functions: http://www.newrock.ch/page8.html
/* VBA one procedure including a image
/* PHP this file itselfe to copy the code or download the zip with examples too
/*
/* Usage: 1. Download or copy this php script
/* 2. Check below parameters and change if liked or extend the color types
/* 3. Upload this php script as the codefiles to a folder
/* Codefiles can also be in a subfolder you handover with the calling page as GET options
/* 4. Link this script with the wished GET options (see below)
/*
/* GET-Option: Optional GET-Parameters
/* - path = path with codefiles
/* - index = if set the index will be shown independ of value
/* - scroll option to scroll back on top
/* - example: ReadTextFiles.php?path=excel-functions&typ=php&index=1&scroll=1;index=1"
/*
/* Not implemented: Multiline comments with a combine sign in the end like _ in vba
/*
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// * * * Define default in case no GET variable is defined
$pathcodefiles="./"; // default for the path of the codefiles (same as this php script file)
$flagindex=0; // 1 = Show index with all code parts
$flagscroll=0; // 1 = Add inside scroll links to scroll back to the top
$typ="vba"; // typ of color definition and other customized definitions
// Check is a GET variable defined and overwrite the default
if(isset($_GET['path'])) {// if subpath is set via get then add this
$pathcodefiles = $_GET['path'];
$pathcodefiles = "./".$pathcodefiles."/";
}
if(isset($_GET['index'])) { // if subpath is set via get then add this
$flagindex=1;
}
if(isset($_GET['scroll'])) { // if subpath is set via get then add this
$flagscrolltop=1;
}
if(isset($_GET['typ'])) { // if subpath is set via get then add this
$typ=$_GET['typ'];
}
// any of this standard settings can also e overwritten in the typ section for typ specific handling
// * * * general defaul definitions if not given by the get
$sep = ";"; // separator to use for any list in this program (might use one which is not included in text)
$extension=".txt"; // extension of the code files (read all this files for show the code) - can be overwriten by the type part
$linestartignorelist="//;-->"; // list of linestarts which had to be ignored to print (space in front get ignored)
$titleindex="Index"; // titlename for the index (if flag index =1)
// * * * define default code comment character and color of comment
$codeidcomment="'"; // as soon as this sign get found it set the rest of the line as comment
$codecolorcomment="Green"; // any webcolor names like Silver or the color code like #808080
// * * * Setting for special lines (no code)
$textline=array(); // initiate the array for the settings
// The start Key-word gets removed from the line - only show the text after.
// be aware it will add the comment sign set by the code typ to be able to use this additional text already in the normal code without changing
// $classline[]="[Start key word as reference];[text size including px];[text color (webcolor)];[1=bold];[1=italic]";
$textline[1]=$codeidcomment."Title:;23px;Black;1;0"; // text line example: Title: This is the Code Title for next Block
$textline[2]=$codeidcomment."Description:;17px;Black;0;1"; // text line example: Description: This is the Code Description for next Block
// * * * Setting image reference
// path to the images to show - if empty then same path as this file
$pathimage=$pathcodefiles; // set same as the code files
// Key text in the text filw with the code followed by the image name
// images get not sized -> size them before and copy the image
$keyimage=$codeidcomment."Image:"; // text line example: 'Image:code1.jpg // can e overwriten in types
// * * * Setting download file reference
// path to the file for download
$pathdownload=$pathcodefiles; // set same as the code files
$downloadtxt="Download"; // Text for download links - add filename too
// Key text in the text filw with the code followed by the image name
// images get not sized -> size them before and copy the image
$keydownload=$codeidcomment."Download:"; // text line example: 'Image:code1.jpg // can e overwriten in types
// * * * Settings for standard format or text
$stdtextcolor="Black"; // standard color set in body (any webcolor names or colorcodes like #808080 )
$codecolorstd="Black"; // standard color for code blocks (any webcolor names or colorcodes like #808080 )
// * * * Settings for inside scroll
// flag needs to be set on top or given by get
$flagscroll=1; // if flag = 1 it will add the inside scroll posibility (usefull with index and many code parts)
$scrollidref=$codeidcomment."Title:"; // This id need to be defined as Keyword, will add the comment character in front automated
$scrolltoptext="(scroll to top)"; // text after the in array as [1]
$scrollid="scroll"; // just used as the id with for scrolling to (add block number later)
$topid="top"; // idname used to scroll back to top
// * * * Settings for comment coloring
$codeidcomment="'"; // for inline comments - as soon as this sign get found it set the rest of the line as comment
$codeidcommentstart="/*"; // for comments with start and end tag
$codeidcommentend="*/"; // for comments with start and end tag
$codecolorcomment="Green"; // any webcolor names like Silver or the color code like #808080
// * * * Settings default for different color of keywords
// initiate arrays for coloring code
$codecolor=array(); // initiate the array for set different colors on words
// you can set as many '$codecolor[] as you like start the array with 1 and increase for other colors
// the first value is the color (webcolor names like Silver or the color code like #808080)
// then separated by semicolon a list of all words you like to color with this color
$codecolor[1]= "Blue;End;End Sub;End Function;End If;End Select;Select Case;For Each;Sub ; Dim;Function ;If;Else;ElseIf;Case;For;Next"; // class 1 word with space first
// * * * Settings default for color text between quotes // use the flag further down to deactive this at all
// initiate arrays for coloring code
//$codecolorquote=array(); // initiate the array for set different colors on words
$colorquote="Red"; // webcolor names like Silver or the color code like #808080
$regexquotes='/"([^"]+)"/'; // can be used for double quotes
//$regexquotes="/'([^']+)'/"; // can be used for singel quotes
// * * * Settings default for color text start with a defined character
// initiate arrays for coloring code
//$codecolorstarttext=array(); // initiate the array for set different colors on words
$colorstarttext="Silver"; // webcolor names like Silver or the color code like #808080
$regexstarttext='/\$\w+/';
//$regexstarttext='^$+[a-zA-Z0-9]\'; // can be used for double quotes
// * * * Settings for coloring on regex search
$colorregex=array(); // initiate the array for the definitions
// each array contain 2 information, the regex to filter and the to set color in CSS
// use $colorregex[definiton number][1]= regex $colorregex[definiton number][2]= color
// you can set as many regex as you like, start with 1
//$colorregex[1][1]='/"([^"]+)"/'; $colorregex[1][2]="Red"; // regex for words between double quotes
$colorregex[1][1]='/"([^"]*)"/'; $colorregex[1][2]="#996600"; // regex for words between double quotes
//$colorregex[1][1]="/'([^']+)'/"; $colorregex[1][2]="Red";// regex word between singel quotes
// * * * Settings for default flags for be able to deactivate some functions - value (1=will be deactivated)
// can be overwritten in the typ area below for have different settings for different kind of codes
$flagnohtmlheader=0; // includes all up and including the body tag
$flagnohtmlfooter=0; // includes the html closing tag
$flagnoignorelines=0; // check on to ingore lines
$flagnocomment=0; // check on color comment
$flagnokeywords=0; // check on key words starting lines
$flagnocolorkeywords=0; // check on color key words
$flagnokeystartwords=0; // check words starting with a defined character
$flagnobetweenquotes=0; // check words between a character
$flagnoregex=0; // check words on regex expressions
// * * * Settings for costumize on different typ of codes
// typ specific definition - overwrite above setting if required or/and and coloring definitions
if($typ=="vba"){ //vba
$codeidcomment="'";
$linestartignorelist="//";
$codecolor[1]= "#0000ff;End;End Sub;End Function;End If;End Select; Then;Select Case;For Each;Sub ; Dim;Function ;If;Else;ElseIf;Case;For;Next"; // class 1 word with space first
$codecolor[2]= "#6600ff;True;False"; // class 1 word with space first
$colorregex[2][1]='/str\w+./'; $colorregex[2][2]="#cc33ff"; // regex for words starting with as str and have a $ in the end
$colorregex[3][1]='/byt\w+/'; $colorregex[3][2]="#ffa64d"; // regex for words starting with as byt
$colorregex[4][1]='/int\w+./'; $colorregex[4][2]="#ff8c1a"; // regex for words starting with as int and have a % in the end
$colorregex[5][1]='/sng\w+./'; $colorregex[5][2]="#cc6600"; // regex for words starting with as sng and have a ! in the end
}elseif($typ=="php"){ // php
$extension=".php"; // overwrite the file extension
$codeidcomment="//";
$linestartignorelist="-->";
$keyimage=$codeidcomment."Image:";
$keydownload="<!--Download:";
$textline[1]="<!--Title:;33px;Black;1;0";
$textline[2]="<!--Subtitle:;27px;Black;1;0";
$textline[3]="<!-- ;20px;Black;0;1";
$codecolor[1]= "Blue;if;function;foreach;for; as ;else;elseif;case;switch;echo;return";
//$codecolor[1]= "Blue;if;function;foreach;for;else;elseif;case;switch;echo";
$codecolor[2]= "Red;(;);{;};<?php;?>";
$colorregex[2][1]='/\$\w*\[.\]\[.\]/'; $colorregex[2][2]="Grey"; // regex for double arrays starting with $
$colorregex[3][1]='/\$\w*\[.\]/'; $colorregex[3][2]="Grey"; // regex for arrays starting with $
$colorregex[4][1]='/\$\w*/'; $colorregex[4][2]="Grey"; // regex for words starting with $
}
// * * * CSS class name definitions adn some default values for CSS
// CSS classnames (values can be changed without any impact - is just set to be everywhere same)
$codeclassstd="codestd"; // css classname for satandard code
$codeclasscomment="codecomment"; // css classname for comment
$codeclasscolor="codecolor"; // css classname for the coloring words (followed by the index number given by the array)
$classline="classline"; // css classname for non-code lines (followed by the index number given by the array)
$classimage="classimage"; // css classname for images - change css settings direct in the CSS style part
$classdownload="classdownload"; // ccs classname for downloades (in a tag)
$classtoplink="classtoplink"; // css class for inside scroll link - set details directly in the style definition further below
$classindextitle="indextitle"; // Class for index title
$classindexlist="indexslist"; // class for index list
$classdivblock="codeblock"; // class for code block <div>
$codeclasscolorquotes="classquotetext"; // class for quote string color
$codeclasscolorstarttext="classstarttext"; // class for quote string color
$classregex="classregex"; // class start name with a counter behind for every defnition
// CSS default values set as variable to be everywhere same
//$sizeimage="60%";
$fontsizeIndexTitle="30px"; // set in the Inextitle class
$fontsizeIndexlist="17px"; // set in the Inexlist class
$fontsizestd="20px"; // set in the body
$fontsizelinkscrolltop="15px"; // set in class for the scroll top text
$fontsizecode="15px"; // set in the div codeblock
$fontsizestd="20px"; // set in the body
$marginside1="10px"; // in <p> tag and code block
$marginside2="20px"; // in listindex class defined to be more in
$fontnocode="Boogaloo"; // used in body for all standard text
$fontnocodefreegoolge="Boogaloo"; // if it is a google free font enter here the google font family name like "Architects+Daughter" - otherwise let it empty
$fontcode="Courier New, monospace"; // iused in the code div block
$fontcodefreegoolge=""; // if it is a google free font enter here the google font family name like "Architects+Daughter" - otherwise let it empty
// * * * Definitions for the code - do not change this area
$keyline=array(); // define the array for check on non code lines
$linestartignore=explode($sep, $linestartignorelist);
$tagastartstd = "<span class=\"".$codeclassstd."\">";
$tagaendstd = "</span>";
$tagcodecomment="<span class=\"".$codeclasscomment."\">";
$replchar="|"; // used in the process as to identify to color parts
// * * * html header inclusive style
// html header
$htmlheader = "";
$htmlheader .= "<!DOCTYPE html><html><head>";
if($fontnocodefreegoolge!=""){$htmlheader .= "<link href=\"https://fonts.googleapis.com/css2?family=".$fontnocode."&display=swap\" rel=\"stylesheet\">";} // for refere to google font family
if($fontcodefreegoolge!=""){$htmlheader .= "<link href=\"https://fonts.googleapis.com/css2?family=".$fontcode."&display=swap\" rel=\"stylesheet\">";} // for refere to google font family
// * * * CSS style
// - be aware in front of any text here are only tabs allowed no spaces because of <<<
// - any words starting with $ are from the settings refered
// - you can change style as allowed standard CSS
$htmlheader .= <<<EOD
<style>
html {
scroll-behavior: smooth;
}
body {
font-family: $fontnocode;
font-style: normal;
font-size: $fontsizestd;
color: $stdtextcolor;
position: relative;
background-color: white;
}
a{ /* all non code text */
font-family:Boogaloo;
margin-top: 0em;
margin-bottom: 0em;
}
a:link { /* only for scroll link */
color: $stdtextcolor;
}
a:visited { /* only for scroll link */
color: $stdtextcolor;
}
a:hover { /* only for scroll link */
color: $stdtextcolor;
}
a:active { /* only for scroll link */
color: $stdtextcolor;
}
p{ /* text and codeblocks are impacted */
margin-bottom: 0em;
margin-top: 0em;
margin-right: $marginside1;
margin-left: $marginside1;
}
hr{ /* used as separator from index to codeblocks */
margin-left: $marginside1;
display: block;
margin-top: 1em;
margin-bottom: 1em;
border-style: inset;
border-width: 1px;
color: silver;
/*background-color:grey;*/
box-shadow: 0 0 2px 2px rgba(0, 0, 0, 0.2);
}
.toplink{ /* needed for sroll back to top */
font-size: 1px;
margin: 0;
}
.$classdivblock { /* block with code here you can set all for the code */
background-color:#F2F3F4;
-webkit-box-shadow: 3px 3px 5px 6px #808080;
margin-top: 1em;
margin-bottom: 1em;
margin-right: $marginside1;
margin-left: $marginside1;
padding-top: 1em;
padding-right: 3em;
padding-bottom: 1em;
padding-left: 3em;
font-family: $fontcode;
font-style: normal;
text-align: left;
text-decoration: none;
overflow-x: scroll;
display: block;
font-size: $fontsizecode;
}
.$classtoplink{ /* link fo jump back to top */
font-size: $fontsizelinkscrolltop;
font-style: italic;
color: $stdtextcolor;
}
.$classindextitle{ /* format index title */
font-size: $fontsizeIndexTitle;
font-style: bold;
}
.$classindexlist{ /* format index list */
font-size: $fontsizeIndexlist;
margin-left: $marginside2; /* additional margin be more right */
}
a.$classdownload{
background-color: Silver;
box-shadow: 0 0 2px 2px rgba(0, 0, 0, 0.2);
}
img.$classimage{ /* format images block - actual is no block around set */
/*-webkit-box-shadow: 0px 0px 3px 3px silver;*/
background-color: white;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
border-radius: 5px;
text-align: mid;
border: 1px;
margin-top: 1em;
margin-bottom: 1em;
margin-right: $marginside1;
margin-left: $marginside1;
display: block;
padding-top: 1em;
padding-right: 3em;
padding-bottom: 1em;
padding-left: 3em;
}
.$codeclassstd{
color: $codecolorstd;
}
.$codeclasscomment{
color: $codecolorcomment;
}
EOD;
// * * * css style code coloring as many as defined
$countcolordef=count($codecolor);
$dynstyle="";
for ($ic = 1; $ic <= $countcolordef; $ic++) {
$poscolor= strpos($codecolor[$ic],";")+1; // find color part in the string
$color=substr($codecolor[$ic],0,$poscolor); //read color out of the string
$codecolor[$ic]=substr($codecolor[$ic],$poscolor-strlen($codecolor[$ic])); //shorten the string for format words only
// sort arraqy on lenght of words desc to have long words first
$colorformat=explode($sep, $codecolor[$ic]);
usort($colorformat, function($a, $b){
return strlen($a) < strlen($b);
});
$codecolor[$ic]=implode($sep,$colorformat);
// write style in a parameter
$dynstyle .= ".".$codeclasscolor.$ic."{";
$dynstyle .= "color: ".$color;
$dynstyle .= "}";
}
$htmlheader .= $dynstyle;
// * * * css style for the Key word lines as many as defined
$countkeydef=count($textline);
$keyline[$countkeydef+1]=$keyimage;
$keyline[$countkeydef+2]=$keydownload;
$countkeyline=$countkeydef+2;
$dynstyle="";
for ($ic = 1; $ic <= $countkeydef; $ic++) {
$classdef=explode($sep, $textline[$ic]);
$keyline[$ic]=$classdef[0]; // initiated in the definitions to add images already -> start by 1 for further definitions
$dynstyle .= ".".$classline.$ic."{";
$dynstyle .= "font-size: ".$classdef[1].";";
$dynstyle .= "color: ".$classdef[2];
if($classdef[3]=="1"){$dynstyle .= "font-style: bold;";}
if($classdef[4]=="1"){$dynstyle .= "font-style: italic;";}
$dynstyle .= "}";
}
$htmlheader .= $dynstyle;
// * * * css style for regex expressions
$countregex=count($colorregex);
$dynstyle="";
for ($ic = 1; $ic <= $countregex; $ic++) {
//$classdef=explode($sep, $colorregex[$ic]);
if($colorregex[$ic][1]!=""){
if($colorregex[$ic][2]==""){$colorregex[$ic][2]="Grey";}
$dynstyle .= ".".$classregex.$ic."{";
$dynstyle .= "color: ".$colorregex[$ic][2];
$dynstyle .= "}";
}
}
$htmlheader .= $dynstyle;
// close style and header
$htmlheader .= "</style>";
$htmlheader .= "</head><body>";
$htmlfooter = "</body></html>";
// add scroll top
if($flagscroll=1){$htmlheader .= "<a class=\"toplink\" id=\"".$topid."\"></a>";}
if($flagnohtmlheader!=1){echo $htmlheader;} // write html header
//echo "<a>".$test."</a>";
// * * * start with file processing
$n=0; // for count blocks for the inside scrolling
$html=""; // for write html code into
$iscomment=0; // for identify comments with start and end in different lines
$foundbetween=array();
$foundword="";
$index="<p class=\"".$classindextitle."\">".$titleindex."</p>"; // start parameter for read the whole index in (get only written to side if flag is set)
if ( file_exists($pathcodefiles) ) { // check are files in the set path
$files = glob($pathcodefiles."*".$extension); // fill array with all files match the set extension
foreach($files as $file){ // loop files and read line by line
$codepart=0; // paramter for set later is is code or text
$file = fopen($file, "r"); // open read only
while(!feof($file)){ // get line by line until the end is reached
$line= fgets($file);
$trimline=trim($line); // trim line to see is it empty
$lineflagcodecolor=15;
$lineflag=$lineflagcodecolor; // status of what is to do - not read = 0, empty line=2, comment line =5, keyword line=8 , color coding line=15 ($lineflagcodecolor)
$flagfoundcolorstrings=0; // for quote string and character start string
// if line is to ignore it will not write and go to next line
if($flagnoignorelines!=1){
foreach ($linestartignore as $ignorval) {
$linestartignorelen=strlen($ignorval);
if(strlen($trimline)>= $linestartignorelen){
$linepart=substr($trimline,0,$linestartignorelen);
if($ignorval==$linepart){$lineflag=0;}
}
}
} // flag not ignore lines
// check if empty line
if($lineflag==$lineflagcodecolor){
// check empty line
if($trimline==""){ // format empty line
$lineflag=2;
if($codepart==1){
$line="</br>";
}
} // if found empty line
} // if empty line handling is required
// check comment lines full lines set from previouse line -> no keyword line
if($flagnocomment!=1){
if($lineflag==$lineflagcodecolor){
if($iscomment==1){ // set start comment the line before -> check end comment in the end
$lineflag=5;
$line=replacetag($line);
$line = $tagcodecomment.$line.$tagaendstd;
if (str_contains($line, $codeidcommentend)) { // line with end comment get also fully as comment marked
$iscomment=0;
}
}
} // if comment required
} // flag no comment
// check keyword line - start with given Keyword (spaces before are allowed)
if($flagnokeywords!=1){
if($lineflag==$lineflagcodecolor){
// check is it a defined key line and not code
for ($ik = 1; $ik <= $countkeyline; $ik++) {
$value=$keyline[$ik];
//foreach ($keyline as $value) {
if (str_contains($line, $value)) {
if(substr($trimline,0,strlen($value))==$value){ // line start with keyword (spaces get ignored)
$lineflag=8;
$line = str_replace($value, "", $line);
$line=trim($line);
$line=replacetag($line);// replace html tag < and >
// if scrolling is set it checks this key and write scroll id and links
if($value==$scrollidref){
$index .= "<p class=\"".$classindexlist."\">";
if($flagscroll==1){
$n++;
$index .= "<a href=\"#".$scrollid.$n."\">".$line."</a>";
}
$index .= "</p>";
}
// check is it an image
if($value==$keyimage){
$line = "<p class=\"".$classimage."\"><img class=\"".$classimage."\" src=\"".$pathimage.$line."\" alt=\"".$line."\"></p>";
// cehck is it a download
}elseif($value==$keydownload){
$line = "<p class=\"".$classdownload."\"><a class=\"".$classdownload."\" href=\"".$pathimage.$line."\">".$downloadtxt." - ".$line."</a></p>";
// else it is another key word with formating
}else{
if($value==$scrollidref){ // if the key word is also the scroll id it writes the link too else only the line
if($flagscroll==1){ // scroll id
$line = "<p class=\"".$classline.$ik."\" id=\"".$scrollid.$n."\"><a>".$line." </a><a class=\"".$classtoplink."\" href=\"#".$topid."\">".$scrolltoptext."</a></p>";
}else{
$line = "<p class=\"".$classline.$ik."\"><a>".$line."</a></p>";
}
}else{
$line = "<p class=\"".$classline.$ik."\"><a>".$line."</a></p>";
}
}
// check was before code and is the code block to close -> put code end tags before
if($codepart==1){
$codepart=0;
$line="</code></pre></br>".$line;
}
break 1;
} //if start with keyword
} // if contain keyword
} // for all keywords
} // if keyword has to be checked
} // flag no Keywords
// split comment from this line for further processing
if($lineflag==$lineflagcodecolor){
$poscomment=999; // set poscomment higher as ever lines have characters
if($flagnocomment!=1){
$line=replacetag($line);// replace html tag < and >
// check some start comment or inline comment
if (str_contains($line, $codeidcomment)) {
$poscomment= strpos($line,$codeidcomment);
}elseif (str_contains($line, $codeidcommentstart)) {
if (!str_contains($line, $codeidcommentend)) { // line with end comment get also fully as comment marked
$iscomment=1; // if no comment end state set next line as comment
}
$poscomment= strpos($line,$codeidcommentstart);
}
// split comment away of the line
$linelenght = strlen($line);
$commentpart="";
if($linelenght>$poscomment){ // comment in the line
$commentpart=substr($line,$poscomment-$linelenght);
$commentpart=$tagcodecomment.$commentpart.$tagaendstd;
$line = substr($line,0,$poscomment);
}
} // flag no comment
} // if comment required
// replace webtags to be able to be shown before adding further tags
if($lineflag==$lineflagcodecolor){
$line=replacetag($line);// replace html tag < and >
}
// coloring on regex - first part - second part comes after keyword coloring
if($flagnoregex!=1){
$regexwordcounter=0;
if($lineflag==$lineflagcodecolor){
for ($ir = 1; $ir <= $countregex; $ir++) {
if($colorregex[$ir][1]!=""){
if (preg_match_all($colorregex[$ir][1], $line, $matches, PREG_PATTERN_ORDER)) {
$regexwordcounter++;
foreach ($matches[0] as $word) {
$foundcolorstring[$regexwordcounter]=$word;
$foundcolorstringclass[$regexwordcounter]=$classregex.$ir;
$replkey=$replchar.$regexwordcounter.$replchar;
$line=str_replace($word,$replkey,$line);
}
} // if matches
} // if found regex
} // for all regex definitions
} // if check required
} // if flag is no between words
// check coloring
if($lineflag==$lineflagcodecolor){
if($flagnocolorkeywords!=1){
// format colorline with key replace
for ($ci = 1; $ci <= $countcolordef; $ci++) {
$colorformat=explode($sep, $codecolor[$ci]);
$colformat=0;
foreach ($colorformat as $value) {
if (str_contains($line, $value)) {
// if singe character -> just replace all
$valuelenght=strlen($value);
$repltag=$tagaendstd."<span class=\"".$codeclasscolor.$ci."\">".$value.$tagaendstd.$tagastartstd;
if($valuelenght==1){
$line=str_replace($value,$repltag,$line);
//$colorline=str_replace($value,$repl,$colorline);
}else{
$replkey=$replchar.$ci.$replchar.$colformat.$replchar;
$line=str_replace($value,$replkey,$line);
}
} // if contain keyword
$colformat++;
} //for every keyword
} // for every color definition
for ($ci = 1; $ci <= $countcolordef; $ci++) {
$colorformat=explode($sep, $codecolor[$ci]);
$colformat=0;
foreach ($colorformat as $value) {
$replkey=$replchar.$ci.$replchar.$colformat.$replchar;
if (str_contains($line, $replkey)) {
$repltag=$tagaendstd."<span class=\"".$codeclasscolor.$ci."\">".$value.$tagaendstd.$tagastartstd;
//$repl=$tagaendstd."<span class=\"".$codeclasscolor.$ci."\">".$value.$tagaendstd.$tagastartstd;
$line=str_replace($replkey,$repltag,$line);
} // if contain keyword
$colformat++;
} //for every keyword
} // for every color definition
// add start and end tag to the line
$line = $tagastartstd.$line.$tagaendstd;
// add commetn back to the line
$line .= $commentpart;
// if before no code add the start code block tags
if($codepart==0){
$codepart=1;
$line="<pre class=\"".$classdivblock."\"><code>".$line;
}
} // flag no colorkeywords
} // if check for color coding
// coloring on regex - first part - second part comes after keyword coloring
if($regexwordcounter>0){
for ($cr = 1; $cr <= $regexwordcounter; $cr++) {
$replkey=$replchar.$cr.$replchar;
$repltag=$tagaendstd."<span class=\"".$foundcolorstringclass[$cr]."\">".$foundcolorstring[$cr].$tagaendstd.$tagastartstd;
$line=str_replace($replkey,$repltag,$line);
} // for all strings
} // if flag is no between words
if($lineflag>0) {
// write line to html
$html .= $line;
}
} // while line
fclose($file); // close file
if($codepart==1){$html .= "</code></pre></br>";} // write code closure tags
}
}else{ // no file found
echo "path (".$path.") with code files not found";
}
// if index is liked it write the index
if($flagindex==1){echo $index."<hr>";}
echo "<a>".$foundword."</a><br>";
// it writes all the html of the files - in case of liked coloring with start and end tags over more than one line it had to be implemented here
echo $html;
// write footer part
if($flagnohtmlfooter!=1){echo $htmlfooter;}
function replacetag($string){
$string=str_replace("<","<",$string);
$string=str_replace(">",">",$string);
return $string;
}
?>