From 81cb94f3d67f1dd052b55a873ce435bbef84b922 Mon Sep 17 00:00:00 2001 From: poustmal Date: Thu, 2 Aug 2018 13:53:19 +0300 Subject: [PATCH 1/2] Add TextFormat::colorize --- src/pocketmine/utils/TextFormat.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/pocketmine/utils/TextFormat.php b/src/pocketmine/utils/TextFormat.php index d790e8c10..3f0aaaf75 100644 --- a/src/pocketmine/utils/TextFormat.php +++ b/src/pocketmine/utils/TextFormat.php @@ -83,6 +83,10 @@ public static function clean($string, $removeFormat = true){ return str_replace("\x1b", "", preg_replace("/\x1b[\\(\\][[0-9;\\[\\(]+[Bm]/", "", $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 * @@ -582,4 +586,4 @@ public static function addSpaces($string, $resPixelLength, $mode = self::MODE_BO return $string; } -} \ No newline at end of file +} From a786877500e62eb18bb71500131437b8f21f28af Mon Sep 17 00:00:00 2001 From: poustmal Date: Thu, 2 Aug 2018 14:01:18 +0300 Subject: [PATCH 2/2] Add info --- src/pocketmine/utils/TextFormat.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/pocketmine/utils/TextFormat.php b/src/pocketmine/utils/TextFormat.php index 3f0aaaf75..9bc9dcdde 100644 --- a/src/pocketmine/utils/TextFormat.php +++ b/src/pocketmine/utils/TextFormat.php @@ -83,6 +83,14 @@ public static function clean($string, $removeFormat = true){ 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); }