Scripts
Highlight PHP
Highlight je PHP code, je kunt zelf m.b.v. CSS tags je code stijlen. (niet langer de dupe vĂ³on voorgedefinieerde kleurcodes e.d. in de highlight_string of highlight_file functies. Graag feedback!
highlight-php
[code]
<?php
$phpcode = '<?php echo "hallo wereld!"; ?>';
echo highlight_php($phpcode);
// Highlight PHP code
function highlight_php($string, $bAddDefaultCss = false)
{
$sPhpManual = 'http://nl2.php.net/';
$aKeywords = array();
// Keywords
$aKeywords[] = array("<?"); // array("match", "css-class");
$aKeywords[] = array("<?php");
$aKeywords[] = array("?>");
$aKeywords[] = array("array");
$aKeywords[] = array("as");
$aKeywords[] = array("break");
$aKeywords[] = array("class");
$aKeywords[] = array("die");
$aKeywords[] = array("do");
$aKeywords[] = array("else");
$aKeywords[] = array("elseif");
$aKeywords[] = array("exit");
$aKeywords[] = array("extends");
$aKeywords[] = array("false");
$aKeywords[] = array("for");
$aKeywords[] = array("foreach");
$aKeywords[] = array("function");
$aKeywords[] = array("if");
$aKeywords[] = array("in");
$aKeywords[] = array("include");
$aKeywords[] = array("include_once");
$aKeywords[] = array("new");
$aKeywords[] = array("null");
$aKeywords[] = array("or");
$aKeywords[] = array("private");
$aKeywords[] = array("public");
$aKeywords[] = array("require");
$aKeywords[] = array("require_once");
$aKeywords[] = array("return");
$aKeywords[] = array("static");
$aKeywords[] = array("true");
$aKeywords[] = array("unknown");
$aKeywords[] = array("var");
$aKeywords[] = array("while");
$result = '';
if($bAddDefaultCss)
{
$result .= '<style type="text/css">'
. 'code.php { background: #FCFCFC; border: #E8E8E8 solid 1px; color: #000000; display: block; overflow: auto; padding: 20px; white-space: nowrap; }'
. 'code.php .keyword { color: #0000FF; }'
. 'code.php .comment { color: #00A000; }'
. 'code.php .quoted { color: #FF00FF; }'
. 'code.php .variable { color: #009999; }'
. 'code.php .function { color: #FF0000; }'
. '</style>';
}
if(strlen($string) > 0)
{
$expr = '/(\'((\\\\.)|[^\\\\\\\'])*\')|("((\\\\.)|[^\\\\\\\"])*")|(\/\*(\n|.)*?\*\/)|(\/\/[^\\n]+\\n)|(\$[a-z0-9_]+)|([a-z0-9_]+)|(<\?php|<\?|\?>)|([\n|\s]+)|(.)/i';
$matches = array();
preg_match_all($expr, $string, $matches);
// print_r($matches[0]);
for($i = 0; $i < sizeof($matches[0]); $i++)
{
if(strcasecmp($match = $matches[0][$i], "") !== 0)
{
if(preg_match("/^(\/\*((\n|.)*?)\*\/)$/i", $match)) // comment
{
$result .= '<span class="comment">' . stringToHtml($match) . '</span>';
}
elseif(preg_match("/^(\/\/[^\\n]+\\n)$/i", $match)) // comment
{
$result .= '<span class="comment">' . stringToHtml($match) . '</span>';
}
elseif(function_exists($match)) // Quoted text
{
$result .= '<a class="function" href="' . $sPhpManual . stringToHtml($match) . '" target="_blank">' . stringToHtml($match) . '</a>';
}
elseif(preg_match("/^(\\\$[a-z0-9_]+)$/i", $match)) // variable
{
$result .= '<span class="keyword">$</span><span class="variable">' . stringToHtml(substr($match, 1)) . '</span>';
}
elseif(strcasecmp(substr($match, 0, 1), "\"") === 0) // Quoted text
{
$result .= '<span class="quoted">' . stringToHtml($match) . '</span>';
}
elseif(strcasecmp(substr($match, 0, 1), "'") === 0) // Quoted text
{
$result .= '<span class="quoted">' . stringToHtml($match) . '</span>';
}
else
{
$aKeyword = false;
for($j = 0; $j < sizeof($aKeywords); $j++)
{
if(strcasecmp($match, $aKeywords[$j][0]) === 0)
{
$aKeyword = $aKeywords[$j];
break;
}
}
if($aKeyword)
{
$result .= '<span class="' . (isset($aKeyword[1]) ? $aKeyword[1] : 'keyword') . '">' . stringToHtml($match) . '</span>';
}
elseif(is_numeric($match))
{
$result .= '<span class="quoted">' . stringToHtml($match) . '</span>';
}
else
{
$result .= stringToHtml($match);
}
}
}
}
}
return '<code class="php">' . $result . '</code>';
}
?>
[/code]
Reacties
0