Implemented TextFormat::colorize() (#1837)

Implemented TextFormat::colorize()
See 4c46087ffccc7f26835a1fc5bbf2ac60397884f9 for the initial discussion
This commit is contained in:
SOFe 2017-12-31 00:51:04 +08:00 committed by Dylan K. Taylor
parent 3ea72a0bf9
commit af7aef70db

View File

@ -80,6 +80,18 @@ abstract class TextFormat{
return str_replace("\x1b", "", preg_replace("/\x1b[\\(\\][[0-9;\\[\\(]+[Bm]/", "", $string));
}
/**
* Replaces placeholders of § with the correct character. Only valid codes (as in the constants of the TextFormat class) will be converted.
*
* @param string $string
* @param string $placeholder default "&"
*
* @return string
*/
public static function colorize(string $string, string $placeholder = "&") : string{
return preg_replace('/' . preg_quote($placeholder, "/") . '([0-9a-fk-or])/u', TextFormat::ESCAPE . '$1', $string);
}
/**
* Returns an JSON-formatted string with colors/markup
*