_begin',
'/\\(:hltPHPLikeVim:\\)(.*?)\\(:hltPHPLikeVimEnd:\\)/is',
function($m) {
$raw = preg_replace('/(
<\/div>|<:vspace>)/i', '', $m[1]);
$raw = str_replace(array('<', '>', '&'), array('<', '>', '&'), $raw);
return Keep(hltPHPLikeVim($raw));
}
);
#--------------------------------------------------
function hltPHPLikeVim($code) {
$palette = [
0 => '#000000', // Black
1 => '#EF2929', // Red (Strings / Numbers / Booleans)
2 => '#00aa00', // Green (Array / NULL / Global / Types)
3 => '#aa5500', // Brown (Objects)
4 => '#3465a4', // Blue (Comments)
5 => '#aa00aa', // Magenta (PHP Tags / Brackets / Delimiters / Escapes)
6 => '#00aaaa', // Cyan (Variable Names / Function Names)
7 => '#aaaaaa', // Gray (Normal Text)
8 => '#555555', // Dark Gray (Hidden/Ignore)
9 => '#ff5555', // Bright Red
10 => '#55ff55', // Bright Green
11 => '#ffff55', // Bright Yellow (The $ Sign)
12 => '#5555ff', // Bright Blue
13 => '#ff55ff', // Bright Magenta
14 => '#55ffff', // Bright Cyan (Special Variables)
15 => '#ffffff', // White (Keywords / Statements / Arrows)
];
$lines = explode("\n", $code);
$output = "";
$in_php = false;
$in_comment = false;
foreach ($lines as $line) {
$pos = 0;
$len = strlen($line);
$newLine = "";
while ($pos < $len) {
$fragment = substr($line, $pos);
if ($in_comment) {
if (preg_match('/^(.*?\*\/)/', $fragment, $m)) {
$newLine .= '
' . htmlspecialchars($m[1]) . '';
$pos += strlen($m[1]);
$in_comment = false;
} else {
$newLine .= '
' . htmlspecialchars($fragment) . '';
$pos = $len;
}
continue;
}
if (!$in_php) {
if (preg_match('/^(.*?)<\?php/i', $fragment, $m)) {
$newLine .= htmlspecialchars($m[1]);
$newLine .= '
<?php';
$pos += strlen($m[0]);
$in_php = true;
} else {
$newLine .= htmlspecialchars($fragment);
$pos = $len;
}
continue;
}
if ($in_php) {
if (preg_match('/^\?>/', $fragment, $m)) {
$newLine .= '
?>';
$pos += 2;
$in_php = false;
continue;
}
if (preg_match('/^(\/\*)/', $fragment, $m)) {
$in_comment = true;
if (preg_match('/^(\/\*.*?\*\/)/', $fragment, $m2)) {
$newLine .= '
' . htmlspecialchars($m2[1]) . '';
$pos += strlen($m2[1]);
$in_comment = false;
} else {
$newLine .= '
' . htmlspecialchars($fragment) . '';
$pos = $len;
}
continue;
}
if (preg_match('/^(\/\/.*|#.*)/', $fragment, $m)) {
$newLine .= '
' . htmlspecialchars($m[1]) . '';
$pos += strlen($m[1]);
continue;
}
if (preg_match('/^("[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\')/', $fragment, $m)) {
$strContents = $m[1];
$safeStr = str_replace(array('<', '>'), array('<', '>'), $strContents);
$highlightedStr = preg_replace_callback('/(\\\\\\\\|\\\\(?=[\'"]|n|r|t))/', function($esc) use ($palette) {
return '
' . $esc[0] . '';
}, $safeStr);
$newLine .= '
' . $highlightedStr . '';
$pos += strlen($m[1]);
continue;
}
if (preg_match('/^(\$)([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)/', $fragment, $m)) {
$newLine .= '
$' . htmlspecialchars($m[2]) . '';
$pos += strlen($m[0]);
continue;
}
if (preg_match('/^(=>)/', $fragment, $m)) {
$newLine .= '
' . htmlspecialchars($m[1]) . '';
$pos += 2;
continue;
}
// Updated Green Keywords list based on Vim's "Type" and "Structure" groups
if (preg_match('/^(array|NULL|global|static|var|void|callable|iterable|mixed|never)\b/i', $fragment, $m)) {
$newLine .= '
' . htmlspecialchars($m[1]) . '';
$pos += strlen($m[1]);
continue;
}
if (preg_match('/^(if|else|foreach|switch|while|for|return|break|class|function|public|private|echo|case|default|static|new|try|catch|T_[A-Z_]+)\b/', $fragment, $m)) {
$newLine .= '
' . htmlspecialchars($m[1]) . '';
$pos += strlen($m[1]);
continue;
}
if (preg_match('/^([a-zA-Z_][a-zA-Z0-9_]*)(?=\s*\()/', $fragment, $m)) {
$newLine .= '
' . htmlspecialchars($m[1]) . '';
$pos += strlen($m[1]);
continue;
}
if (preg_match('/^(,|;)/', $fragment, $m)) {
$newLine .= '
' . htmlspecialchars($m[1]) . '';
$pos += 1;
continue;
}
if (preg_match('/^(int|string|bool|float|object|self|parent)\b/i', $fragment, $m)) {
$newLine .= '
' . htmlspecialchars($m[1]) . '';
$pos += strlen($m[1]);
continue;
}
if (preg_match('/^(\d+|0x[0-9a-fA-F]+|TRUE|FALSE)\b/i', $fragment, $m)) {
$newLine .= '
' . htmlspecialchars($m[1]) . '';
$pos += strlen($m[1]);
continue;
}
if (preg_match('/^([\[\]\(\)\{\}])/', $fragment, $m)) {
$newLine .= '
' . htmlspecialchars($m[1]) . '';
$pos += 1;
continue;
}
$char = substr($line, $pos, 1);
$newLine .= htmlspecialchars($char);
$pos++;
}
}
$output .= $newLine . "\n";
}
return '
' . rtrim($output) . '
';
}
#--------------------------------------------------