GenisysPro  for Minecraft PE/Windows 10 v1.1.x
Feature-rich server software for Minecraft PE and Windows 10 Edition
TextFormat Class Reference

Static Public Member Functions

static tokenize ($string)
 
static clean ($string, $removeFormat=true)
 
static toJSON ($string)
 
static toHTML ($string)
 
static toANSI ($string)
 

Data Fields

const ESCAPE = "\xc2\xa7"
 
const BLACK = TextFormat::ESCAPE . "0"
 
const DARK_BLUE = TextFormat::ESCAPE . "1"
 
const DARK_GREEN = TextFormat::ESCAPE . "2"
 
const DARK_AQUA = TextFormat::ESCAPE . "3"
 
const DARK_RED = TextFormat::ESCAPE . "4"
 
const DARK_PURPLE = TextFormat::ESCAPE . "5"
 
const GOLD = TextFormat::ESCAPE . "6"
 
const GRAY = TextFormat::ESCAPE . "7"
 
const DARK_GRAY = TextFormat::ESCAPE . "8"
 
const BLUE = TextFormat::ESCAPE . "9"
 
const GREEN = TextFormat::ESCAPE . "a"
 
const AQUA = TextFormat::ESCAPE . "b"
 
const RED = TextFormat::ESCAPE . "c"
 
const LIGHT_PURPLE = TextFormat::ESCAPE . "d"
 
const YELLOW = TextFormat::ESCAPE . "e"
 
const WHITE = TextFormat::ESCAPE . "f"
 
const OBFUSCATED = TextFormat::ESCAPE . "k"
 
const BOLD = TextFormat::ESCAPE . "l"
 
const STRIKETHROUGH = TextFormat::ESCAPE . "m"
 
const UNDERLINE = TextFormat::ESCAPE . "n"
 
const ITALIC = TextFormat::ESCAPE . "o"
 
const RESET = TextFormat::ESCAPE . "r"
 

Detailed Description

Class used to handle Minecraft chat format, and convert it to other formats like ANSI or HTML

Member Function Documentation

◆ clean()

static clean (   $string,
  $removeFormat = true 
)
static

Cleans the string from Minecraft codes and ANSI Escape Codes

Parameters
string$string
bool$removeFormat
Returns
mixed
73  {
74  if($removeFormat){
75  return str_replace(TextFormat::ESCAPE, "", preg_replace(["/" . TextFormat::ESCAPE . "[0123456789abcdefklmnor]/", "/\x1b[\\(\\][[0-9;\\[\\(]+[Bm]/"], "", $string));
76  }
77  return str_replace("\x1b", "", preg_replace("/\x1b[\\(\\][[0-9;\\[\\(]+[Bm]/", "", $string));
78  }

◆ toANSI()

static toANSI (   $string)
static

Returns a string with colorized ANSI Escape codes

Parameters
$string
Returns
string
391  {
392  if(!is_array($string)){
393  $string = self::tokenize($string);
394  }
395 
396  $newString = "";
397  foreach($string as $token){
398  switch($token){
399  case TextFormat::BOLD:
400  $newString .= Terminal::$FORMAT_BOLD;
401  break;
403  $newString .= Terminal::$FORMAT_OBFUSCATED;
404  break;
405  case TextFormat::ITALIC:
406  $newString .= Terminal::$FORMAT_ITALIC;
407  break;
409  $newString .= Terminal::$FORMAT_UNDERLINE;
410  break;
412  $newString .= Terminal::$FORMAT_STRIKETHROUGH;
413  break;
414  case TextFormat::RESET:
415  $newString .= Terminal::$FORMAT_RESET;
416  break;
417 
418  //Colors
419  case TextFormat::BLACK:
420  $newString .= Terminal::$COLOR_BLACK;
421  break;
423  $newString .= Terminal::$COLOR_DARK_BLUE;
424  break;
426  $newString .= Terminal::$COLOR_DARK_GREEN;
427  break;
429  $newString .= Terminal::$COLOR_DARK_AQUA;
430  break;
432  $newString .= Terminal::$COLOR_DARK_RED;
433  break;
435  $newString .= Terminal::$COLOR_PURPLE;
436  break;
437  case TextFormat::GOLD:
438  $newString .= Terminal::$COLOR_GOLD;
439  break;
440  case TextFormat::GRAY:
441  $newString .= Terminal::$COLOR_GRAY;
442  break;
444  $newString .= Terminal::$COLOR_DARK_GRAY;
445  break;
446  case TextFormat::BLUE:
447  $newString .= Terminal::$COLOR_BLUE;
448  break;
449  case TextFormat::GREEN:
450  $newString .= Terminal::$COLOR_GREEN;
451  break;
452  case TextFormat::AQUA:
453  $newString .= Terminal::$COLOR_AQUA;
454  break;
455  case TextFormat::RED:
456  $newString .= Terminal::$COLOR_RED;
457  break;
459  $newString .= Terminal::$COLOR_LIGHT_PURPLE;
460  break;
461  case TextFormat::YELLOW:
462  $newString .= Terminal::$COLOR_YELLOW;
463  break;
464  case TextFormat::WHITE:
465  $newString .= Terminal::$COLOR_WHITE;
466  break;
467  default:
468  $newString .= $token;
469  break;
470  }
471  }
472 
473  return $newString;
474  }

◆ toHTML()

static toHTML (   $string)
static

Returns an HTML-formatted string with colors/markup

Parameters
string | array$string
Returns
string
275  {
276  if(!is_array($string)){
277  $string = self::tokenize($string);
278  }
279  $newString = "";
280  $tokens = 0;
281  foreach($string as $token){
282  switch($token){
283  case TextFormat::BOLD:
284  $newString .= "<span style=font-weight:bold>";
285  ++$tokens;
286  break;
288  //$newString .= "<span style=text-decoration:line-through>";
289  //++$tokens;
290  break;
291  case TextFormat::ITALIC:
292  $newString .= "<span style=font-style:italic>";
293  ++$tokens;
294  break;
296  $newString .= "<span style=text-decoration:underline>";
297  ++$tokens;
298  break;
300  $newString .= "<span style=text-decoration:line-through>";
301  ++$tokens;
302  break;
303  case TextFormat::RESET:
304  $newString .= str_repeat("</span>", $tokens);
305  $tokens = 0;
306  break;
307 
308  //Colors
309  case TextFormat::BLACK:
310  $newString .= "<span style=color:#000>";
311  ++$tokens;
312  break;
314  $newString .= "<span style=color:#00A>";
315  ++$tokens;
316  break;
318  $newString .= "<span style=color:#0A0>";
319  ++$tokens;
320  break;
322  $newString .= "<span style=color:#0AA>";
323  ++$tokens;
324  break;
326  $newString .= "<span style=color:#A00>";
327  ++$tokens;
328  break;
330  $newString .= "<span style=color:#A0A>";
331  ++$tokens;
332  break;
333  case TextFormat::GOLD:
334  $newString .= "<span style=color:#FA0>";
335  ++$tokens;
336  break;
337  case TextFormat::GRAY:
338  $newString .= "<span style=color:#AAA>";
339  ++$tokens;
340  break;
342  $newString .= "<span style=color:#555>";
343  ++$tokens;
344  break;
345  case TextFormat::BLUE:
346  $newString .= "<span style=color:#55F>";
347  ++$tokens;
348  break;
349  case TextFormat::GREEN:
350  $newString .= "<span style=color:#5F5>";
351  ++$tokens;
352  break;
353  case TextFormat::AQUA:
354  $newString .= "<span style=color:#5FF>";
355  ++$tokens;
356  break;
357  case TextFormat::RED:
358  $newString .= "<span style=color:#F55>";
359  ++$tokens;
360  break;
362  $newString .= "<span style=color:#F5F>";
363  ++$tokens;
364  break;
365  case TextFormat::YELLOW:
366  $newString .= "<span style=color:#FF5>";
367  ++$tokens;
368  break;
369  case TextFormat::WHITE:
370  $newString .= "<span style=color:#FFF>";
371  ++$tokens;
372  break;
373  default:
374  $newString .= $token;
375  break;
376  }
377  }
378 
379  $newString .= str_repeat("</span>", $tokens);
380 
381  return $newString;
382  }

◆ toJSON()

static toJSON (   $string)
static

Returns an JSON-formatted string with colors/markup

Parameters
string | array$string
Returns
string
87  {
88  if(!is_array($string)){
89  $string = self::tokenize($string);
90  }
91  $newString = [];
92  $pointer =& $newString;
93  $color = "white";
94  $bold = false;
95  $italic = false;
96  $underlined = false;
97  $strikethrough = false;
98  $obfuscated = false;
99  $index = 0;
100 
101  foreach($string as $token){
102  if(isset($pointer["text"])){
103  if(!isset($newString["extra"])){
104  $newString["extra"] = [];
105  }
106  $newString["extra"][$index] = [];
107  $pointer =& $newString["extra"][$index];
108  if($color !== "white"){
109  $pointer["color"] = $color;
110  }
111  if($bold !== false){
112  $pointer["bold"] = true;
113  }
114  if($italic !== false){
115  $pointer["italic"] = true;
116  }
117  if($underlined !== false){
118  $pointer["underlined"] = true;
119  }
120  if($strikethrough !== false){
121  $pointer["strikethrough"] = true;
122  }
123  if($obfuscated !== false){
124  $pointer["obfuscated"] = true;
125  }
126  ++$index;
127  }
128  switch($token){
129  case TextFormat::BOLD:
130  if($bold === false){
131  $pointer["bold"] = true;
132  $bold = true;
133  }
134  break;
136  if($obfuscated === false){
137  $pointer["obfuscated"] = true;
138  $obfuscated = true;
139  }
140  break;
141  case TextFormat::ITALIC:
142  if($italic === false){
143  $pointer["italic"] = true;
144  $italic = true;
145  }
146  break;
148  if($underlined === false){
149  $pointer["underlined"] = true;
150  $underlined = true;
151  }
152  break;
154  if($strikethrough === false){
155  $pointer["strikethrough"] = true;
156  $strikethrough = true;
157  }
158  break;
159  case TextFormat::RESET:
160  if($color !== "white"){
161  $pointer["color"] = "white";
162  $color = "white";
163  }
164  if($bold !== false){
165  $pointer["bold"] = false;
166  $bold = false;
167  }
168  if($italic !== false){
169  $pointer["italic"] = false;
170  $italic = false;
171  }
172  if($underlined !== false){
173  $pointer["underlined"] = false;
174  $underlined = false;
175  }
176  if($strikethrough !== false){
177  $pointer["strikethrough"] = false;
178  $strikethrough = false;
179  }
180  if($obfuscated !== false){
181  $pointer["obfuscated"] = false;
182  $obfuscated = false;
183  }
184  break;
185 
186  //Colors
187  case TextFormat::BLACK:
188  $pointer["color"] = "black";
189  $color = "black";
190  break;
192  $pointer["color"] = "dark_blue";
193  $color = "dark_blue";
194  break;
196  $pointer["color"] = "dark_green";
197  $color = "dark_green";
198  break;
200  $pointer["color"] = "dark_aqua";
201  $color = "dark_aqua";
202  break;
204  $pointer["color"] = "dark_red";
205  $color = "dark_red";
206  break;
208  $pointer["color"] = "dark_purple";
209  $color = "dark_purple";
210  break;
211  case TextFormat::GOLD:
212  $pointer["color"] = "gold";
213  $color = "gold";
214  break;
215  case TextFormat::GRAY:
216  $pointer["color"] = "gray";
217  $color = "gray";
218  break;
220  $pointer["color"] = "dark_gray";
221  $color = "dark_gray";
222  break;
223  case TextFormat::BLUE:
224  $pointer["color"] = "blue";
225  $color = "blue";
226  break;
227  case TextFormat::GREEN:
228  $pointer["color"] = "green";
229  $color = "green";
230  break;
231  case TextFormat::AQUA:
232  $pointer["color"] = "aqua";
233  $color = "aqua";
234  break;
235  case TextFormat::RED:
236  $pointer["color"] = "red";
237  $color = "red";
238  break;
240  $pointer["color"] = "light_purple";
241  $color = "light_purple";
242  break;
243  case TextFormat::YELLOW:
244  $pointer["color"] = "yellow";
245  $color = "yellow";
246  break;
247  case TextFormat::WHITE:
248  $pointer["color"] = "white";
249  $color = "white";
250  break;
251  default:
252  $pointer["text"] = $token;
253  break;
254  }
255  }
256 
257  if(isset($newString["extra"])){
258  foreach($newString["extra"] as $k => $d){
259  if(!isset($d["text"])){
260  unset($newString["extra"][$k]);
261  }
262  }
263  }
264 
265  return json_encode($newString, JSON_UNESCAPED_SLASHES);
266  }

◆ tokenize()

static tokenize (   $string)
static

Splits the string by Format tokens

Parameters
string$string
Returns
array
61  {
62  return preg_split("/(" . TextFormat::ESCAPE . "[0123456789abcdefklmnor])/", $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
63  }

Field Documentation

◆ AQUA

const AQUA = TextFormat::ESCAPE . "b"

◆ BLACK

const BLACK = TextFormat::ESCAPE . "0"

◆ BLUE

const BLUE = TextFormat::ESCAPE . "9"

◆ BOLD

const BOLD = TextFormat::ESCAPE . "l"

◆ DARK_AQUA

const DARK_AQUA = TextFormat::ESCAPE . "3"

◆ DARK_BLUE

const DARK_BLUE = TextFormat::ESCAPE . "1"

◆ DARK_GRAY

const DARK_GRAY = TextFormat::ESCAPE . "8"

◆ DARK_GREEN

const DARK_GREEN = TextFormat::ESCAPE . "2"

◆ DARK_PURPLE

const DARK_PURPLE = TextFormat::ESCAPE . "5"

◆ DARK_RED

const DARK_RED = TextFormat::ESCAPE . "4"

◆ ESCAPE

const ESCAPE = "\xc2\xa7"

◆ GOLD

const GOLD = TextFormat::ESCAPE . "6"

◆ GRAY

const GRAY = TextFormat::ESCAPE . "7"

◆ GREEN

const GREEN = TextFormat::ESCAPE . "a"

◆ ITALIC

const ITALIC = TextFormat::ESCAPE . "o"

◆ LIGHT_PURPLE

const LIGHT_PURPLE = TextFormat::ESCAPE . "d"

◆ OBFUSCATED

const OBFUSCATED = TextFormat::ESCAPE . "k"

◆ RED

const RED = TextFormat::ESCAPE . "c"

◆ RESET

const RESET = TextFormat::ESCAPE . "r"

◆ STRIKETHROUGH

const STRIKETHROUGH = TextFormat::ESCAPE . "m"

◆ UNDERLINE

const UNDERLINE = TextFormat::ESCAPE . "n"

◆ WHITE

const WHITE = TextFormat::ESCAPE . "f"

◆ YELLOW

const YELLOW = TextFormat::ESCAPE . "e"

The documentation for this class was generated from the following file: