TextFormat: use mb_scrub() in clean()

this redacts invalid characters to prevent them appearing in places that might break the client.
This commit is contained in:
Dylan K. Taylor 2019-02-13 20:01:36 +00:00
parent 7a6f279825
commit 8fad5a6e30

View File

@ -25,6 +25,7 @@ namespace pocketmine\utils;
use function is_array;
use function json_encode;
use function mb_scrub;
use function preg_quote;
use function preg_replace;
use function preg_split;
@ -77,14 +78,15 @@ abstract class TextFormat{
}
/**
* Cleans the string from Minecraft codes and ANSI Escape Codes
* Cleans the string from Minecraft codes, ANSI Escape Codes and invalid UTF-8 characters
*
* @param string $string
* @param bool $removeFormat
*
* @return string
* @return string valid clean UTF-8
*/
public static function clean(string $string, bool $removeFormat = true) : string{
$string = mb_scrub($string, 'UTF-8');
if($removeFormat){
return str_replace(TextFormat::ESCAPE, "", preg_replace(["/" . TextFormat::ESCAPE . "[0-9a-fk-or]/u", "/\x1b[\\(\\][[0-9;\\[\\(]+[Bm]/"], "", $string));
}