-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Another BBCode Helper
Billy Delouche edited this page May 28, 2013
·
30 revisions
Category:Helper::Community| Category:Helper::Text
I wanted a bbCode helper similar to the original smiley helper (wich is so usefull) but i couldn't find it. This is my first Ignited code and i hope it will help someone. Remarks are welcome on the forum topic.
This helper can actualy :
- Generate a javascript function needed to insert bbCodes into a form field
- Parse bbCode
- Clear bbCode tags
- Generate an array of bbCode buttons that can be clicked to be inserted
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter
*
* An open source application development framework for PHP 4.3.2 or newer
*
* @package CodeIgniter
* @author Rick Ellis
* @copyright Copyright (c) 2006, EllisLab, Inc.
* @license http://www.codeignitor.com/user_guide/license.html
* @link http://www.codeigniter.com
* @since Version 1.0
* @filesource
*/
// ------------------------------------------------------------------------
/**
* CodeIgniter bbCode Helpers
*
* @package CodeIgniter
* @subpackage Helpers
* @category Helpers
* @author Santoni Jean-André
*/
// ------------------------------------------------------------------------
/**
* JS Insert bbCode
*
* Generates the javascrip function needed to insert bbcodes into a form field
*
* @access public
* @param string form name
* @param string field name
* @return string
*/
function js_insert_bbcode($form_name = '', $form_field = '')
{
?>
<script type="text/javascript">
function insert_bbcode(bbopen, bbclose)
{
var input = window.document.<?=$form_name.'.'.$form_field; ?>;
input.focus();
/* for Internet Explorer )*/
if(typeof document.selection != 'undefined')
{
var range = document.selection.createRange();
var insText = range.text;
range.text = bbopen + insText + bbclose;
range = document.selection.createRange();
if (insText.length == 0)
{
range.move('character', -bbclose.length);
}
else
{
range.moveStart('character', bbopen.length + insText.length + bbclose.length);
}
range.select();
}
/* for newer browsers like Firefox */
else if(typeof input.selectionStart != 'undefined')
{
var start = input.selectionStart;
var end = input.selectionEnd;
var insText = input.value.substring(start, end);
input.value = input.value.substr(0, start) + bbopen + insText + bbclose + input.value.substr(end);
var pos;
if (insText.length == 0)
{
pos = start + bbopen.length;
}
else
{
pos = start + bbopen.length + insText.length + bbclose.length;
}
input.selectionStart = pos;
input.selectionEnd = pos;
}
/* for other browsers like Netscape... */
else
{
var pos;
var re = new RegExp('^[0-9]{0,3}$');
while(!re.test(pos))
{
pos = prompt("insertion (0.." + input.value.length + "):", "0");
}
if(pos > input.value.length)
{
pos = input.value.length;
}
var insText = prompt("Please tape your text");
input.value = input.value.substr(0, pos) + bbopen + insText + bbclose + input.value.substr(pos);
}
}
</script>
<?php
}
// ------------------------------------------------------------------------
/**
* Parse bbCode
*
* Takes a string as input and replace bbCode by (x)HTML tags
*
* @access public
* @param string the text to be parsed
* @return string
*/
function parse_bbcode($str, $clear = 0, $bbcode_to_parse = NULL)
{
if ( ! is_array($bbcode_to_parse))
{
if (FALSE === ($bbcode_to_parse = _get_bbcode_to_parse_array()))
{
return FALSE;
}
}
foreach ($bbcode_to_parse as $key => $val)
{
for ($i = 1; $i <= $bbcode_to_parse[$key][2]; $i++) // loop for imbricated tags
{
$str = preg_replace($key, $bbcode_to_parse[$key][$clear], $str);
}
}
return $str;
}
// ------------------------------------------------------------------------
/**
* Clear bbCode
*
* Takes a string as input and remove bbCode tags
*
* @access public
* @param string the text to be parsed
* @return string
*/
function clear_bbcode($str)
{
return parse_bbcode($str, 1);
}
// ------------------------------------------------------------------------
/**
* Get bbCode Buttons
*
* Returns an array of bbcode buttons that can be clicked to be inserted
* into a form field.
*
* @access public
* @return array
*/
function get_bbcode_buttons($bbcode = NULL)
{
if ( ! is_array($bbcode))
{
if (FALSE === ($bbcode = _get_bbcode_array()))
{
return $str;
}
}
foreach ($bbcode as $key => $val)
{
$button[] = '<input type="button" class="button" id="'.$key.'" name="'.$key.'" value="'.$key.'" onClick="'.$val.'" />';
}
return $button;
}
// ------------------------------------------------------------------------
/**
* Get bbCode Array
*
* Fetches the config/bbcode.php file
*
* @access private
* @return mixed
*/
function _get_bbcode_array()
{
if ( ! file_exists(APPPATH.'config/bbcode'.EXT))
{
return FALSE;
}
include(APPPATH.'config/bbcode'.EXT);
if ( ! isset($bbcode) OR ! is_array($bbcode))
{
return FALSE;
}
return $bbcode;
}
// ------------------------------------------------------------------------
/**
* Get bbCode Array for parsing
*
* Fetches the config/bbcode.php file
*
* @access private
* @return mixed
*/
function _get_bbcode_to_parse_array()
{
if ( ! file_exists(APPPATH.'config/bbcode'.EXT))
{
return FALSE;
}
include(APPPATH.'config/bbcode'.EXT);
if ( ! isset($bbcode_to_parse) OR ! is_array($bbcode_to_parse))
{
return FALSE;
}
return $bbcode_to_parse;
}
?>
And here is the config/bbcode.php file :
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/*
| -------------------------------------------------------------------
| BBCODE
| -------------------------------------------------------------------
| This file contains two arrays of bbcode for use with the bbcode helper.
| The first array is for buttons and the second is for parsing.
|
*/
$bbcode = array(
// name onClick
"b" => "[removed]insert_bbcode('**', '**');return(false)",
"i" => "[removed]insert_bbcode('*', '*');return(false)",
"u" => "[removed]insert_bbcode('[u]', '[/u]');return(false)",
"center" => "[removed]insert_bbcode('[center]', '[/center]');return(false)",
"right" => "[removed]insert_bbcode('[right]', '[/right]');return(false)",
"justify" => "[removed]insert_bbcode('[justify]', '[/justify]');return(false)",
"quote" => "[removed]insert_bbcode('[q=AUTHOR]', '[/q]');return(false)",
"img" => "[removed]insert_bbcode('[img]', '[/img]');return(false)",
"url" => "[removed]insert_bbcode('[', ']()');return(false)",
"email" => "[removed]insert_bbcode('[email=]', '[/email]');return(false)"
);
$bbcode_to_parse = array(
// regex replacement clean loop
"#\[base_url\]#i" => array(base_url(), base_url(), 1),
"#\[/\]#" => array("<hr width=\"100%\" size=\"1\" />", "", 1),
"#\[hr\]i#" => array("<hr width=\"100%\" size=\"1\" />", "", 1),
"#\[b\](.+)\[/b\]#isU" => array("<strong>$1</strong>", "", 1),
"#\[i\](.+)\[/i\]#isU" => array("<em>$1</em>", "", 1),
"#\[u\](.+)\[/u\]#isU" => array("<u>$1</u>", "", 1),
"#\[center\](.+)\[/center\]#isU" => array("<div style=\"text-align: center\">$1</div>", "", 1),
"#\[right\](.+)\[/right\]#isU" => array("<div style=\"text-align: right\">$1</div>", "", 1),
"#\[justify\](.+)\[/justify\]#isU" => array("<div style=\"text-align: justify\">$1</div>", "", 1),
"#\[color=(.+)\](.+)\[/color\]#isU" => array("<span style=\"color:$1\">$2</span>", "", 1),
"#\[size=([0-9]+)\](.+)\[/size\]#isU" => array("<span style=\"font-size:$1px\">$2</span>", "", 1),
"#\[img\](.+)\[/img\]#isU" => array("<img />", "", 1),
"#\[img=(.+)\]#isU" => array("<img />", "", 1),
"#\[email\](.+)\[/email\]#isU" => array("<a >$1</a>", "$1", 1),
"#\[email=(.+)\](.+)\[/email\]#isU" => array("<a >$2</a>", "$1 ($2)", 1),
"#\[url\](.+)\[/url\]#isU" => array("<a >$1</a>", "$1", 1),
"#\[url=(.+)\](.+)\[/url\]#isU" => array("<a >$2</a>", "$1 ($2)", 1),
"#\[list\](.+)\[/list\]#isU" => array("<ul>$1</ul>", "\n$1\n", 1),
"#\[\*\](.+)\[/\*\]#isU" => array("<li>$1</li>", " - $1\n", 1),
"#\[q\](.+)\[/q\]#isU" => array("<blockquote>$1</blockquote>", "\"$1\"", 5),
"#\[q=(.+)\](.+)\[/q\]#isU" => array("<blockquote cite=\"$1\">$2</blockquote>", "\"$2\" ($1)", 5),
);
?>