Skip to content

Instantly share code, notes, and snippets.

@ladaposamuel
Forked from neo22s/bbcode.php
Created December 5, 2015 22:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ladaposamuel/c55627b7fa4c920e6e7d to your computer and use it in GitHub Desktop.
Save ladaposamuel/c55627b7fa4c920e6e7d to your computer and use it in GitHub Desktop.
BBcode parser example
<?php
/**
* BBcode helper class
*
* @package BBcode
* @category Helper
* @author Chema <chema@garridodiaz.com>
* @copyright (c) 2012
* @license GPL v3
*/
class bbcode {
/**
*
* This function parses BBcode tag to HTML code (XHTML transitional 1.0)
*
* It parses (only if it is in valid format e.g. an email must to be
* as example@example.ext or similar) the text with BBcode and
* translates in the relative html code.
*
* @param string $text
* @param boolean $advanced his var describes if the parser run in advanced mode (only *simple* bbcode is parsed).
* @return string
*/
public static function tohtml($text,$advanced=FALSE,$charset='utf8'){
//special chars
$text = htmlspecialchars($text, ENT_QUOTES,$charset);
/**
* This array contains the main static bbcode
* @var array $basic_bbcode
*/
$basic_bbcode = array(
'[b]', '[/b]',
'[i]', '[/i]',
'[u]', '[/u]',
'[s]','[/s]',
'[ul]','[/ul]',
'[li]', '[/li]',
'[ol]', '[/ol]',
'[center]', '[/center]',
'[left]', '[/left]',
'[right]', '[/right]',
);
/**
* This array contains the main static bbcode's html
* @var array $basic_html
*/
$basic_html = array(
'<b>', '</b>',
'<i>', '</i>',
'<u>', '</u>',
'<s>', '</s>',
'<ul>','</ul>',
'<li>','</li>',
'<ol>','</ol>',
'<div style="text-align: center;">', '</div>',
'<div style="text-align: left;">', '</div>',
'<div style="text-align: right;">', '</div>',
);
/**
*
* Parses basic bbcode, used str_replace since seems to be the fastest
*/
$text = str_replace($basic_bbcode, $basic_html, $text);
//advanced BBCODE
if ($advanced)
{
/**
* This array contains the advanced static bbcode
* @var array $advanced_bbcode
*/
$advanced_bbcode = array(
'#\[color=([a-zA-Z]*|\#?[0-9a-fA-F]{6})](.+)\[/color\]#Usi',
'#\[size=([0-9][0-9]?)](.+)\[/size\]#Usi',
'#\[quote](\r\n)?(.+?)\[/quote]#si',
'#\[quote=(.*?)](\r\n)?(.+?)\[/quote]#si',
'#\[url](.+)\[/url]#Usi',
'#\[url=(.+)](.+)\[/url\]#Usi',
'#\[email]([\w\.\-]+@[a-zA-Z0-9\-]+\.?[a-zA-Z0-9\-]*\.\w{1,4})\[/email]#Usi',
'#\[email=([\w\.\-]+@[a-zA-Z0-9\-]+\.?[a-zA-Z0-9\-]*\.\w{1,4})](.+)\[/email]#Usi',
'#\[img](.+)\[/img]#Usi',
'#\[img=(.+)](.+)\[/img]#Usi',
'#\[code](\r\n)?(.+?)(\r\n)?\[/code]#si',
'#\[youtube]http://[a-z]{0,3}.youtube.com/watch\?v=([0-9a-zA-Z]{1,11})\[/youtube]#Usi',
'#\[youtube]([0-9a-zA-Z]{1,11})\[/youtube]#Usi'
);
/**
* This array contains the advanced static bbcode's html
* @var array $advanced_html
*/
$advanced_html = array(
'<span style="color: $1">$2</span>',
'<span style="font-size: $1px">$2</span>',
"<div class=\"quote\"><span class=\"quoteby\">Disse:</span>\r\n$2</div>",
"<div class=\"quote\"><span class=\"quoteby\">Disse <b>$1</b>:</span>\r\n$3</div>",
'<a rel="nofollow" target="_blank" href="$1">$1</a>',
'<a rel="nofollow" target="_blank" href="$1">$2</a>',
'<a href="mailto: $1">$1</a>',
'<a href="mailto: $1">$2</a>',
'<img src="$1" alt="$1" />',
'<img src="$1" alt="$2" />',
'<div class="code">$2</div>',
'<object type="application/x-shockwave-flash" style="width: 450px; height: 366px;" data="http://www.youtube.com/v/$1"><param name="movie" value="http://www.youtube.com/v/$1" /><param name="wmode" value="transparent" /></object>',
'<object type="application/x-shockwave-flash" style="width: 450px; height: 366px;" data="http://www.youtube.com/v/$1"><param name="movie" value="http://www.youtube.com/v/$1" /><param name="wmode" value="transparent" /></object>'
);
$text = preg_replace($advanced_bbcode, $advanced_html,$text);
}
//before return convert line breaks to HTML
return bbcode::nl2br($text);
}
/**
*
* removes bbcode from text
* @param string $text
* @return string text cleaned
*/
public static function remove($text)
{
return strip_tags(str_replace(array('[',']'), array('<','>'), $text));
}
/**
*
* Inserts HTML line breaks before all newlines in a string
* @param string $var
*/
public static function nl2br($var)
{
return str_replace(array('\\r\\n','\r\\n','r\\n','\r\n', '\n', '\r'), '<br />', nl2br($var));
}
}
<?php
if ($_POST)
{
include 'bbcode.php';
//parses posted BBcode to HTML
$html_code = bbcode::tohtml($_POST['description'],TRUE);
//removes the bbcode
$nobbcode = bbcode::remove($_POST['description']);
}
?>
<!doctype html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>bbcode example</title>
<!-- Le HTML5 shim, for IE6-8 support of HTML elements -->
<!--[if lt IE 9]> <script type="text/javascript" src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> <![endif]-->
<link rel="stylesheet" href="http://sceditor.samclarke.com/minified/jquery.sceditor.min.css" type="text/css" media="all" />
</head>
<body>
<div style="margin:0 auto 0 auto;width:900px;">
<h1>PHP BBcode parser.</h1>
<form action="" method="post">
<textarea name="description" id="description" style="width: 100%; height: 400px"><?=$_POST['description']?></textarea>
<input type="submit" value="Convert to HTML" />
</form>
<?if($_POST):?>
<h4>Parsed HTML</h4>
<?=$html_code?>
<h4>HTML source code</h4>
<textarea style="width: 100%; height: 100px"><?=$html_code?></textarea>
<h4>Posted BBcode</h4>
<textarea style="width: 100%; height: 100px"><?=$_POST['description']?></textarea>
<h4>Remove BBcode</h4>
<textarea style="width: 100%; height: 100px"><?=$nobbcode?></textarea>
<?endif?>
<p>This example uses the super nice WYSIWYG JQuery BBcode <a href="https://github.com/samclarke/SCEditor">SCEditor</a></p>
</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="http://sceditor.samclarke.com/minified/jquery.sceditor.min.js"></script>
<script>
$(function(){
$('textarea[name=description]').sceditorBBCodePlugin({
toolbar: "bold,italic,underline,strike,|left,center,right,justify|" +
"size,color,removeformat|bulletlist,orderedlist|email,link,unlink,youtube|source",
resizeEnabled: "true"
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment