highlight-php
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?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>';
}
?>
$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>';
}
?>