Skip to content

Instantly share code, notes, and snippets.

@uintdev
Last active February 4, 2017 18:53
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 uintdev/d02171f4a03a1d7e1149c5fd5b572ad4 to your computer and use it in GitHub Desktop.
Save uintdev/d02171f4a03a1d7e1149c5fd5b572ad4 to your computer and use it in GitHub Desktop.
PHP BBCode Parser w/ line break support
<?php
function bb_parse($string) {
$string = str_replace("\r\n", "\r", $string);
$tags = 'b|i|s|u|rain';
while (preg_match_all('`\[('.$tags.')=?(.*?)\](.+?)\[/\1\]`', $string, $matches)) foreach ($matches[0] as $key => $match) {
list($tag, $param, $innertext) = array($matches[1][$key], $matches[2][$key], $matches[3][$key]);
switch ($tag) {
case 'b': $replacement = "<b>$innertext</b>"; break;
case 'i': $replacement = "<i>$innertext</i>"; break;
case 's': $replacement = "<s>$innertext</s>"; break;
case 'u': $replacement = "<u>$innertext</u>"; break;
case 'rain': $replacement = "<span style=\"background-image: -webkit-gradient( linear, left top, right top, color-stop(0, #f22), color-stop(0.15, #f2f), color-stop(0.3, #22f), color-stop(0.45, #2ff), color-stop(0.6, #2f2),color-stop(0.75, #2f2), color-stop(0.9, #ff2), color-stop(1, #f22) );
background-image: gradient( linear, left top, right top, color-stop(0, #f22), color-stop(0.15, #f2f), color-stop(0.3, #22f), color-stop(0.45, #2ff), color-stop(0.6, #2f2),color-stop(0.75, #2f2), color-stop(0.9, #ff2), color-stop(1, #f22) );color:transparent;-webkit-background-clip: text;background-clip: text;\">$innertext</span>"; break;
}
$string = str_replace($match, $replacement, $string);
}
return $string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment