Skip to content

Instantly share code, notes, and snippets.

@lcherone
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lcherone/3285cbf0fc8574967f6f to your computer and use it in GitHub Desktop.
Save lcherone/3285cbf0fc8574967f6f to your computer and use it in GitHub Desktop.
de'BBCODE'er
<?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