[PHP-NOTES] note 130364 added to function.highlight-string

[email protected] ("[email protected]")
Newsgroups php.notes
Message-ID <[email protected]>
Function highlight_string() in its pure form is IMHO rather messy.
But it can be easily made tidy and more handy with minor changes and CSS use:

● Line numbers of code (based on HTML <ol> & <li>, so they don't mess selecting code)
● Optional loading code from a file
● Tidy code:
  ● Replacing style attributes with use of CSS classes
  ● Removing unnecessary tags
  ● Countless &nbsp; to spaces (CSS handles that) etc.

<?php
/**
 * Code syntax highlighting
 * 
 * @version   2025.06.12
 * @author    Walerian Walawski
 * @link      https://w87.eu/
 * 
 * @param  string|null $file — path of a file to load or null to use OB
 */

function w87HighlightCode($file = null)
{
	static $w87HighlightCodeOn = false;

	ini_set('highlight.comment', 'comment');
	ini_set('highlight.default', 'default');
	ini_set('highlight.html', 'html');
	ini_set('highlight.keyword', 'keyword');
	ini_set('highlight.string', 'string');

	if(!$file && !$w87HighlightCodeOn){
		ob_start();
		$w87HighlightCodeOn = true;
	}else{
		$code = highlight_string(trim($file ? file_get_contents($file) : "<?php\n".ob_get_contents()."\n?>"), true);
		$code = preg_replace(["|^\\<code\\>\\<span style\\=\"color\\: (.+){2,24}\"\\>|", "|\\</code\\>\$|", "|\\</span\\>\$|"], '', trim($code), 1);
		$code = str_replace([' style="color: ', '<br />', '&nbsp;'], [' class="w87-hc-', '</li><li>', ' '], trim($code));

		if(!$file) ob_end_clean();
		$w87HighlightCodeOn = false;

		return '<ol class="w87-hc-lines"><li>'.$code.'</li></ol>';
	}
}

/** -----------------------------------------------------------------------------
 * Use the function above with the CSS included in your styles file or in <head>:
 * ------------------------------------------------------------------------------

<style>
.w87-hc-lines{background:#06121F;color:#FFFFFF;
font-family:monospace;white-space:pre-wrap;word-wrap:break-word;padding:4px 6px;margin:-2px 0 4px 45px;line-height:18px;}
.w87-hc-lines li{background:#09192B;padding:1px 0 0 5px;font-family:Monospace;}
.w87-hc-lines li:nth-child(odd){background:#112233;}
.w87-hc-comment{color: #FFAA00;}
.w87-hc-default{color: #AAFF00;}
.w87-hc-html{color: #00EEFF;}
.w87-hc-keyword{color: #FF2277; font-weight: bold;}
.w87-hc-string{color: #FFFFFF;}
</style>

 */
?>

Using to highlight code in a file:

<?php
echo w87HighlightCode(__DIR__.'/fileName.php');
?>

Using with output buffering for handy embedding code:
 
<?php echo '<h3>This is a test code example</h3>';
w87HighlightCode(); ?>
echo "Now 'quetes' and $other \n $special chars. don't require 'escaping'!";
<?php echo w87HighlightCode(); ?>
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.137 (proxied: 89.234.212.36)
----
Manual Page -- https://php.net/manual/en/function.highlight-string.php
Edit        -- https://main.php.net/note/edit/130364
Del: integrated  -- https://main.php.net/note/delete/130364/integrated
Del: useless     -- https://main.php.net/note/delete/130364/useless
Del: bad code    -- https://main.php.net/note/delete/130364/bad+code
Del: spam        -- https://main.php.net/note/delete/130364/spam
Del: non-english -- https://main.php.net/note/delete/130364/non-english
Del: in docs     -- https://main.php.net/note/delete/130364/in+docs
Del: other reasons-- https://main.php.net/note/delete/130364
Reject      -- https://main.php.net/note/reject/130364
Search      -- https://main.php.net/manage/user-notes.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.