Skip to content

Instantly share code, notes, and snippets.

@goosewoman
Last active August 29, 2015 14:07
Show Gist options
  • Save goosewoman/d1291833ddf21df3e2f3 to your computer and use it in GitHub Desktop.
Save goosewoman/d1291833ddf21df3e2f3 to your computer and use it in GitHub Desktop.
a simple method for converting minecraft colourcodes into HTML
.c0
{
color : #000000;
}
.c1
{
color : #0000AA;
}
.c2
{
color : #00AA00;
}
.c3
{
color : #00AAAA;
}
.c4
{
color : #AA0000;
}
.c5
{
color : #AA00AA;
}
.c6
{
color : #FFAA00;
}
.c7
{
color : #AAAAAA;
}
.c8
{
color : #555555;
}
.c9
{
color : #5555FF;
}
.ca
{
color : #55FF55;
}
.cb
{
color : #55FFFF;
}
.cc
{
color : #FF5555;
}
.cd
{
color : #FF55FF;
}
.ce
{
color : #FFFF55;
}
.cf
{
color : #FFFFFF;
}
.cl
{
font-weight : 900;
}
.co
{
font-style : italic;
}
.cn
{
text-decoration : underline;
}
.cm
{
text-decoration : line-through;
}
.cr
{
color : #FFFFFF;
font-weight : normal;
font-style : normal;
text-decoration : none;
}
<?php
/*
Copyright (c) 2013 Luuk Jacobs.
You may not copy or redistribute this work without written consent of the copyright holder.
You may not use this work commercially.
You are required to give credit when you are releasing/distributing anything that uses this work.
*/
/**
*
* @param $matches
* @return string
*/
function allCallback( $matches )
{
$matches[1] = strtolower( $matches[1] );
$bindings = array(
0 => 'c0',
1 => 'c1',
2 => 'c2',
3 => 'c3',
4 => 'c4',
5 => 'c5',
6 => 'c6',
7 => 'c7',
8 => 'c8',
9 => 'c9',
'a' => 'ca',
'b' => 'cb',
'c' => 'cc',
'd' => 'cd',
'e' => 'ce',
'f' => 'cf',
'r' => 'cr',
);
$bold = array(
'l' => 'cl',
'r' => 'cr',
);
$italic = array(
'o' => 'co',
'r' => 'cr',
);
$decoration = array(
'n' => 'cn',
'm' => 'cm',
'r' => 'cr',
);
$fg = isset( $bindings[$matches[1]] ) ? $bindings[$matches[1]] : 'cr';
$bo = isset( $bold[$matches[1]] ) ? $bold[$matches[1]] : 'cr';
$it = isset( $italic[$matches[1]] ) ? $italic[$matches[1]] : 'cr';
$de = isset( $decoration[$matches[1]] ) ? $decoration[$matches[1]] : 'cr';
if ( isset( $bindings[$matches[1]] ) )
{
return "<span class='$fg'>" . $matches[2] . "</span>";
}
if ( isset( $bold[$matches[1]] ) )
{
return "<span class='$bo'>" . $matches[2] . "</span>";
}
if ( isset( $italic[$matches[1]] ) )
{
return "<span class='$it'>" . $matches[2] . "</span>";
}
if ( isset( $decoration[$matches[1]] ) )
{
return "<span class='$de'>" . $matches[2] . "</span>";
}
return "";
}
/**
* @param $matches
* @return string
*/
function formattingCallback( $matches )
{
$matches[1] = strtolower( $matches[1] );
$bold = array(
'l' => 'cl',
'r' => 'cr',
);
$italic = array(
'o' => 'co',
'r' => 'cr',
);
$decoration = array(
'n' => 'cn',
'm' => 'cm',
'r' => 'cr',
);
$bo = isset( $bold[$matches[1]] ) ? $bold[$matches[1]] : 'cr';
$it = isset( $italic[$matches[1]] ) ? $italic[$matches[1]] : 'cr';
$de = isset( $decoration[$matches[1]] ) ? $decoration[$matches[1]] : 'cr';
if ( isset( $bold[$matches[1]] ) )
{
return "<span class='$bo'>";
}
if ( isset( $italic[$matches[1]] ) )
{
return "<span class='$it'>";
}
if ( isset( $decoration[$matches[1]] ) )
{
return "<span class='$de'>";
}
return "";
}
/**
* @param $text
* @return string
*/
function mctohtml( $text )
{
$text = preg_replace( '(\n)', "", $text );
$text = preg_replace( '/§([k-or0-9a-f])/i', "&$1", $text );
$text = "&f" . trim( $text ) . "&r";
$colorSplit = preg_split( "/(&[k-or0-9a-f])/i", $text, NULL, PREG_SPLIT_DELIM_CAPTURE );
$spans = 0;
$partialHTML = "";
foreach ( $colorSplit as $string )
{
switch ( $string )
{
case'&l':
$spans++;
$string = preg_replace_callback( '/&(l)/i', "formattingCallback", $string );
break;
case'&m':
$spans++;
$string = preg_replace_callback( '/&(m)/i', "formattingCallback", $string );
break;
case'&n':
$spans++;
$string = preg_replace_callback( '/&(n)/i', "formattingCallback", $string );
break;
case'&o':
$spans++;
$string = preg_replace_callback( '/&(o)/i', "formattingCallback", $string );
break;
}
$span = str_repeat( "</span>", $spans );
$string = preg_replace( "/(&[0-9a-fr])/i", $span . '$1', $string );
$partialHTML .= $string;
}
$html = preg_replace_callback( '/&([0-9a-fr])((.*?)((?=(&[0-9a-fr]))))/i', 'allCallback', $partialHTML );
$html = preg_replace( '(&r|&k)', '', $html );
$html .= "<br />";
return $html;
}
$text = "§6An &lexample &fof §4colourcodes §l§n§obeing§4 changed"
print mctohtml($text);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment