Last active
August 29, 2015 14:01
-
-
Save lcherone/3285cbf0fc8574967f6f to your computer and use it in GitHub Desktop.
de'BBCODE'er
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function bbdecoder($bbcode){ | |
/* Match url */ | |
$urlmatch = "([a-zA-Z]+[:\/\/]+[A-Za-z0-9\-_:]+\\.+[A-Za-z0-9\.\/%&=\?\-_:]+)"; | |
/* Basically remove HTML tag's functionality */ | |
$bbcode = htmlspecialchars($bbcode); | |
/* Bold text */ | |
$match["b"] = "/\[b\](.*?)\[\/b\]/is"; | |
$replace["b"] = "<span style=\"font-weight:bold;\">$1</span>"; | |
/* Italics */ | |
$match["i"] = "/\[i\](.*?)\[\/i\]/is"; | |
$replace["i"] = "<i>$1</i>"; | |
/* Underline */ | |
$match["u"] = "/\[u\](.*?)\[\/u\]/is"; | |
$replace["u"] = "<span style=\"text-decoration: underline\">$1</span>"; | |
/* Typewriter text */ | |
$match["tt"] = "/\[tt\](.*?)\[\/tt\]/is"; | |
$replace["tt"] = "<span style=\"font-family:monospace;\">$1</span>"; | |
$match["ttext"] = "/\[ttext\](.*?)\[\/ttext\]/is"; | |
$replace["ttext"] = "<span style=\"font-family:monospace;\">$1</span>"; | |
/* Strikethrough text */ | |
$match["s"] = "/\[s\](.*?)\[\/s\]/is"; | |
$replace["s"] = "<span style=\"text-decoration: line-through;\">$1</span>"; | |
$match["strike"] = "/\[strike\](.*?)\[\/strike\]/is"; | |
$replace["strike"] = "<span style=\"text-decoration: line-through;\">$1</span>"; | |
/* Color (or Colour) */ | |
$match["color"] = "/\[color=([a-zA-Z]+|#[a-fA-F0-9]{3}[a-fA-F0-9]{0,3})\](.*?)\[\/color\]/is"; | |
$replace["color"] = "<span style=\"color: $1\">$2</span>"; | |
$match["colour"] = "/\[colour=([a-zA-Z]+|#[a-fA-F0-9]{3}[a-fA-F0-9]{0,3})\](.*?)\[\/colour\]/is"; | |
$replace["colour"] = $replace["color"]; | |
/* Size */ | |
$match["size"] = "/\[size=([0-9]+(%|px|em)?)\](.*?)\[\/size\]/is"; | |
$replace["size"] = "<span style=\"font-size: $1;\">$3</span>"; | |
/* Images */ | |
$match["img"] = "/\[img\]".$urlmatch."\[\/img\]/is"; | |
$replace["img"] = "<img src=\"$1\" style=\"max-width:300px;\"/>"; | |
/* Links */ | |
$match["url"] = "/\[url=".$urlmatch."\](.*?)\[\/url\]/is"; | |
$replace["url"] = "<a href=\"$1\">$2</a>"; | |
$match["surl"] = "/\[url\]".$urlmatch."\[\/url\]/is"; | |
$replace["surl"] = "<a href=\"$1\">$1</a>"; | |
/* Quotes */ | |
$match["quote"] = "/\[quote\](.*?)\[\/quote\]/ism"; | |
$replace["quote"] = "<blockquote>$1</blockquote>"; | |
/* Parse */ | |
$bbcode = preg_replace($match, $replace, $bbcode); | |
/* New line to <br> tag */ | |
$bbcode = nl2br($bbcode); | |
/* Return */ | |
return $bbcode; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment