Thu, December 2, 2021
 
https://devcodepro.comdevcodepro
X
InterServer Web Hosting and VPS
Home · Latest · Trends
16

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 protected]]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 16 times (16) (0)
comments: 0 / hits: 3325  / 2 years ago, wed, dec 11, 19, 10:46:24
More From » PHP
 

Comments

There are no comments for this Snippet yet
Only authorized users can post. Please sign in first, or register a free account
Login with Google
 
Share
Posted
pport
Member since Dec 11, 2019
Total Code Snippets: 7
Total Comments: 0
Location: Hartford, Connecticut
pport snippets
1 year ago, tue, nov 24, 20, 8:31:52
comments: 0 / hits: 311
2 years ago, thu, dec 26, 19, 1:40:52
comments: 0 / hits: 2326
2 years ago, wed, feb 12, 20, 2:14:06
comments: 0 / hits: 3919
1 year ago, tue, nov 24, 20, 8:40:57
comments: 0 / hits: 415
CSS
12 months ago, sun, jan 3, 21, 3:39:57
comments: 0 / hits: 211
CSS
12 months ago, mon, jan 4, 21, 11:38:12
comments: 0 / hits: 205