Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TextFormat::colorize #624

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/pocketmine/utils/TextFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ 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);
}

/**
* Returns an JSON-formatted string with colors/markup
*
Expand Down Expand Up @@ -582,4 +594,4 @@ public static function addSpaces($string, $resPixelLength, $mode = self::MODE_BO
return $string;
}

}
}