note 70220 added to function.highlight-file

From: Date: Sat, 07 Oct 2006 01:38:01 +0000
Subject: note 70220 added to function.highlight-file
Groups: php.notes 
Request: Send a blank email to php-notes+get-118166@lists.php.net to get a copy of this message
Hi there, i build a "little" class to controll the highlighting functions (highlight_source & highlight_file) of PHP a little bit more. You can: - set the highlighting colors without rewrite your php.ini -create a whitelist using by highlightFile - so your're surly that nobody take a look on your private source ;-) - work with the highlighted content - it returns the highlighted content, not show it like highlight_file or highlight_source did. Class: <?php class betterHighlighting { /* user colors */ private $userColors = array( "string" => "", "comment" => "", "keyword" => "", "bg" => "", "default" => "", "html" => "", ); /* array of allowed file for using by ::highlightFile() */ private $whitelist = array(); /* construct */ function __construct($allowedFiles = NULL, /* array or NULL */ $newColors = NULL /* array or NULL */) { /* add (if some given) files to the highlighting whitelist */ if($allowedFiles) { $this -> addFiles($allowedFiles); } /* set highlighting colors */ if($newColors) { $this -> setColors($newColors); } } /* for adding files to the whitelist */ public function addFiles($allowedFiles /* array or string */) { $file = is_array($allowedFiles) ? array_pop($allowedFiles) : $allowedFiles; if(file_exists($file)) { $this -> whitelist[] = $file; } else if(is_array($allowedFiles) && sizeof($allowedFiles)) { $this -> addFiles($allowedFiles); } return void; } /* for setting new userColors */ public function setColors($newColors = array()) { foreach($newColors as $key => $color) { if(in_array($key, array_keys($this -> userColors))) { $this -> userColors[$key] = $color; } } } /* replaces default colors with users colors */ private function putColors($highlightedContent) { $nHC = $highlightedContent; $hTpl = "<span style=\"color: %s\">"; foreach($this -> userColors as $key => $color) { if(!empty($color)) { $nHC = str_replace(sprintf($hTpl, ini_get("highlight.{$key}")), sprintf($hTpl, $color), $nHC); } } return $nHC; } /* highlight's a string */ public function highlightString($string) { $hC = ""; ob_start(); highlight_string($string); $hC = ob_get_contents(); ob_end_clean(); return $this -> putColors($hC); } /* highlight's a file */ public function highlightFile($file) { if(in_array($file, $this -> whitelist)) { return $this -> highlightString(join("", file($file))); } return ""; } } ?> Usage: $cl = new betterHighlighting([mixed files], [mixed colors]); To put a file or a array of files to the whitelist, use (void) ::addFiles(mixed files). To set a new color for highlighting use (void) ::setColors(array("string" => ..., "comment" => ..., "keyword" => ..., "bg" => ..., "default" => ..., "html" => ...)). You only have to give the keyword(s), which color you'll change. If you only want to change the color of the highlighted comments, call ::setColors(array("comment" => "newcolor")) and so on. Highlighting String: (string)::highlightString(string $source) Highlighting File: (string)::highlightFile(string $file) Note: The two last function RETURNS the highlighted content, not shows it directly. Little example: I want to highlight the file "test.php" and the comments should be pink. <?php $cl = betterHighlighting("test.php", array("comment" => "pink")); echo $cl -> highlightFile("test.php"); ?> Have fun. ---- Server IP: 217.160.72.57 Probable Submitter: 80.134.228.128 ---- Manual Page -- http://www.php.net/manual/en/function.highlight-file.php Edit -- https://master.php.net/note/edit/70220 Del: integrated -- https://master.php.net/note/delete/70220/integrated Del: useless -- https://master.php.net/note/delete/70220/useless Del: bad code -- https://master.php.net/note/delete/70220/bad+code Del: spam -- https://master.php.net/note/delete/70220/spam Del: non-english -- https://master.php.net/note/delete/70220/non-english Del: in docs -- https://master.php.net/note/delete/70220/in+docs Del: other reasons-- https://master.php.net/note/delete/70220 Reject -- https://master.php.net/note/reject/70220 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#118166) next »