Sun, January 19, 2020, 11:46:33
 
Home · Latest · Trends  /  PHP
11

PHP BBCode parser

<?php
//based on bbcode2htm plugin
//$mytext = '[font=Georgia]Font family example[/font] <br />
//[color=#BB0000]Font color example[/color] <br />
//[size=24]Font size example[/size] <br />
//[b]Bold text[/b] <br />
//[i]Italic text[/i] <br />
//[u]Underlined text[/u] <br />
//[s]Text with a strikethrough[/s] <br />
//[small]Small defines smaller text[/small] <br />
//[align=right]Align right example[/align] <br />
//[align=center]Center example[/align] <br />
//[code]//foreach ($array as $item) {
//  echo "$item\n";
//  $array[] = $item;
//}
//[/code] <br />
//[php]
//foreach ($array as $item) {
//  echo "$item\n";
//  $array[] = $item;
//}
//[/php] <br />
//[quote]blockquote - HTML Block Quotation Element[/quote] <br />
//This is[sub] subscript[/sub] and [sup]superscript[/sup] example <br />
//[email=webmaster@example.com]Email link example[/email] <br />
//[img]https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png[/img] <br />
//[url=https://www.google.com]Link example[/url] <br />';
function show_BBcode($code) {
 $input = array(
  '/(?<!\\\\)\[font(?::\w+)?=(.*?)\](.*?)\[\/font(?::\w+)?\]/si' => "<span style=\"font-family:\\1\">\\2</span>",
  '/(?<!\\\\)\[color(?::\w+)?=(.*?)\](.*?)\[\/color(?::\w+)?\]/si' => "<span style=\"color:\\1\">\\2</span>",
  '/(?<!\\\\)\[size(?::\w+)?=(.*?)\](.*?)\[\/size(?::\w+)?\]/si' => "<span style=\"font-size:\\1\">\\2</span>",
  '/(?<!\\\\)\[b(?::\w+)?\](.*?)\[\/b(?::\w+)?\]/si' => "<strong>\\1</strong>",
  '/(?<!\\\\)\[i(?::\w+)?\](.*?)\[\/i(?::\w+)?\]/si' => "<i>\\1</i>",
  '/(?<!\\\\)\[u(?::\w+)?\](.*?)\[\/u(?::\w+)?\]/si' => "<u>\\1</u>",
  '/(?<!\\\\)\[s(?::\w+)?\](.*?)\[\/s(?::\w+)?\]/si' => "<s>\\1</s>",
  '/(?<!\\\\)\[small(?::\w+)?\](.*?)\[\/small(?::\w+)?\]/si' => "<small>\\1</small>",
  '/(?<!\\\\)\[align(?::\w+)?=(.*?)\](.*?)\[\/align(?::\w+)?\]/si' => "<div style=\"text-align:\\1\">\\2</div>",
  '/(?<!\\\\)\[center(?::\w+)?\](.*?)\[\/center(?::\w+)?\]/si' => "<div style=\"text-align:center\">\\1</div>",
  '/(?<!\\\\)\[code(?::\w+)?\](.*?)\[\/code(?::\w+)?\]/si' => "<div style=\"font-family: Consolas,Monaco,Lucida Console,monospace;font-size: 13px;background-color:#eff0f1;padding:10px;\">Code:<pre class=\"prettyprint linenums\">\\1</pre></div>",
  '/(?<!\\\\)\[php(?::\w+)?\](.*?)\[\/php(?::\w+)?\]/si' => "<div style=\"font-family: 'Courier New',Consolas,Monaco,monospace;font-size: 12px;background-color:#eff0f1;padding:10px;\">Code:<pre class=\"prettyprint linenums\">\\1</pre></div>",
  '/(?<!\\\\)\[quote(?::\w+)?\](.*?)\[\/quote(?::\w+)?\]/si' => "<blockquote style=\"font-size: 15px;background-color:lightyellow;padding:16px;\">\\1</blockquote>",
  '/(?<!\\\\)\[sub(?::\w+)?\](.*?)\[\/sub(?::\w+)?\]/si' => "<sub>\\1</sub>",
  '/(?<!\\\\)\[sup(?::\w+)?\](.*?)\[\/sup(?::\w+)?\]/si' => "<sup>\\1</sup>",
  '/(?<!\\\\)\[email(?::\w+)?=(.*?)\](.*?)\[\/email(?::\w+)?\]/si' => "<a href=\"mailto:\\1\">\\2</a>",
  '/(?<!\\\\)\[img(?::\w+)?\](.*?)\[\/img(?::\w+)?\]/si' => "<img src=\"\\1\" alt=\"\\1\" />",
  '/(?<!\\\\)\[url(?::\w+)?=(.*?)?\](.*?)\[\/url(?::\w+)?\]/si' => "<a href=\"\\1\" target=\"_blank\">\\2</a>");
 $code = preg_replace(array_keys($input), array_values($input), $code);
 return $code;
}
//echo show_BBcode($mytext);
?>
rated 11 times [  11] [ 0]  / comments: 0 / hits: 135  / 1 month ago, wed, dec 11, 2019, 22:46:24
More From » PHP
 

Comments

There are no comments for this Snippet yet
Username
 
Your Comment
 
Share
Snippets
Posted
pport

pport

Member since Dec 11, 2019
Total Code Snippets: 2
Total Comments: 0
Location: n/a
Following
User not following anyone yet.