Skip to content

Instantly share code, notes, and snippets.

@uuf6429
Created April 8, 2017 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uuf6429/b6df80e2aedd80dd9e5af4cf5575795a to your computer and use it in GitHub Desktop.
Save uuf6429/b6df80e2aedd80dd9e5af4cf5575795a to your computer and use it in GitHub Desktop.
<?php
if ($argc !== 2) {
die('File to highlight was expected as first argument.');
}
$replace = [
// base styles
'EMER' => ["\e[45;30m", " \e[0m "],
'ALRT' => ["\e[43;30m", " \e[0m "],
'CRIT' => ["\e[41;30m", " \e[0m "],
'ERRR' => ["\e[40;31m", " \e[0m "],
'WARN' => ["\e[40;33m", " \e[0m "],
'NOTE' => ["\e[40;36m", " \e[0m "],
'INFO' => ["\e[40;34m", " \e[0m "],
'DBUG' => ["\e[40;37m", " \e[0m "],
// aliases
'EMERGENCY' => 'EMER',
'ALERT' => 'ALRT',
'CRITICAL' => 'CRIT',
'ERROR' => 'ERRR',
'WARNING' => 'WARN',
'NOTICE' => 'NOTE',
'INFORMATION' => 'INFO',
'DEBUG' => 'DBUG',
];
echo preg_replace_callback(
sprintf(
'/^(.*?)\\s(%s)\\s-\\s/m',
implode('|', array_keys($replace))
),
function ($matches) use ($replace) {
$style = $replace[$matches[2]];
if(is_string($style)){
$style = $replace[$style];
}
return sprintf(
'%s%s %s%s ',
$style[0],
$matches[1],
$matches[2],
$style[1]
);
},
file_get_contents($argv[1])
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment