Skip to content

Another BBCode Helper

World Wide Web Server edited this page Jul 4, 2012 · 30 revisions

Category:Helpers

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

[code]<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); /**

// ------------------------------------------------------------------------

/**

  • 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);
    }
} 
&lt;/script&gt; 
&lt;?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($t) { // horizontal bar $t=str_replace("[/]", "<hr width="100%" size="1" />", $t); $t=str_replace("[hr]", "<hr width="100%" size="1" />", $t);

// bold
$t=str_replace("[b]", "<strong>", $t);
$t=str_replace("[/b]", "</strong>", $t);

// italic
$t=str_replace("[i]", "<em>", $t);
$t=str_replace("[/i]", "</em>", $t);

// underline
$t=str_replace("[u]", "<u>", $t);
$t=str_replace("[/u]", "</u>", $t);

// quotes
for ($i = 1; $i <= 5; $i++) // loop for imbricated quotes
{
    $regQuoteSimple="\[q\] ?([^\[]*) ?\[/q\]";
    $regQuoteEtendu="\[q ?=([^\[]*) ?] ?([^]]*) ?\[/q\]";
    if (ereg($regQuoteSimple, $t)) $t=ereg_replace($regQuoteSimple, "<blockquote>\\1</blockquote>", $t);
    else $t=ereg_replace($regQuoteEtendu, "<blockquote cite=\"\\1\">\\2</blockquote>", $t);
}

// center
$t=str_replace("[center]", "<div style=\"text-align: center\">", $t);
$t=str_replace("[/center]", "</div>", $t);

// right
$t=str_replace("[right]", "<div style=\"text-align: right\">", $t);
$t=str_replace("[/right]", "</div>", $t);

// justify
$t=str_replace("[justify]", "<div style=\"text-align: justify\">", $t);
$t=str_replace("[/justify]", "</div>", $t);

// color
$t=str_replace("[/color]", "</span>", $t);
$regCouleur="\[color= ?(([[:alpha:]]+)|(#[[:digit:][:alpha:]]{6})) ?\]";
$t=ereg_replace($regCouleur, "<span style=\"color: \\1\">", $t);

// characters size
$t=str_replace("[/size]", "</span>", $t);
$regCouleur="\[size= ?([[:digit:]]+) ?\]";
$t=ereg_replace($regCouleur, "<span style=\"font-size: \\1px\">", $t);

// link
$regLienSimple="\[url\] ?([^\[]*) ?\[/url\]";
$regLienEtendu="\[url ?=([^\[]*) ?] ?([^]]*) ?\[/url\]";
if (ereg($regLienSimple, $t)) $t=ereg_replace($regLienSimple, "<a href=\"\\1\">\\1</a>", $t);
else $t=ereg_replace($regLienEtendu, "<a href=\"\\1\" target=\"_blank\">\\2</a>", $t);

// link (upper case tag)
$regLienSimple="\[URL\] ?([^\[]*) ?\[/URL\]";
$regLienEtendu="\[URL ?=([^\[]*) ?] ?([^]]*) ?\[/URL\]";
if (ereg($regLienSimple, $t)) $t=ereg_replace($regLienSimple, "<a href=\"\\1\">\\1</a>", $t);
else $t=ereg_replace($regLienEtendu, "<a href=\"\\1\" target=\"_blank\">\\2</a>", $t);

// email
$regMailSimple="\[email\] ?([^\[]*) ?\[/email\]";
$regMailEtendu="\[email ?=([^\[]*) ?] ?([^]]*) ?\[/email\]";
if (ereg($regMailSimple, $t)) $t=ereg_replace($regMailSimple, "<a href=\"mailto:\\1\">\\1</a>", $t);
else $t=ereg_replace($regMailEtendu, "<a href=\"mailto:\\1\">\\2</a>", $t);

// image
$regImage="\[img\] ?([^\[]*) ?\[/img\]";
$regImageAlternatif="\[img ?= ?([^\[]*) ?\]";
if (ereg($regImage, $t)) $t=ereg_replace($regImage, "<img src=\"\\1\" alt=\"\" border=\"0\" />", $t);
else $t=ereg_replace($regImageAlternatif, "<img src=\"\\1\" alt=\"\" border=\"0\" />", $t);

// list
$t=str_replace("[list]", "<ul>", $t);
$t=str_replace("[/list]", "</ul>", $t);
$t=str_replace("[*]", "<li>", $t);
$t=str_replace("[/*]", "</li>", $t);

// base URL
$t=str_replace("base_url/", base_url(), $t);

return $t;

}

// ------------------------------------------------------------------------

/**

  • 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($t) { // horizontal bar $t=str_replace("[/]", "", $t); $t=str_replace("[hr]", "", $t);

    // bold $t=str_replace("[b]", "", $t); $t=str_replace("[/b]", "", $t);

    // italic $t=str_replace("[i]", "", $t); $t=str_replace("[/i]", "", $t);

    // underline $t=str_replace("[u]", "", $t); $t=str_replace("[/u]", "", $t);

    // quotes for ($i = 1; $i <= 5; $i++) // loop for imbricated quotes { $regQuoteSimple="[q] ?([^[]) ?[/q]"; $regQuoteEtendu="[q ?=([^[]) ?] ?([^]]*) ?[/q]"; if (ereg($regQuoteSimple, $t)) $t=ereg_replace($regQuoteSimple, "<< \1 >>", $t); else $t=ereg_replace($regQuoteEtendu, "<< \2 >> (\1)", $t); }

    // center $t=str_replace("[center]", "", $t); $t=str_replace("[/center]", "", $t);

    // right $t=str_replace("[right]", "", $t); $t=str_replace("[/right]", "", $t);

    // justify $t=str_replace("[justify]", "", $t); $t=str_replace("[/justify]", "", $t);

    // color $t=str_replace("[/color]", "", $t); $regCouleur="[color= ?((:alpha:+)|(#[[:digit:][:alpha:]]{6})) ?]"; $t=ereg_replace($regCouleur, "", $t);

    // characters size $t=str_replace("[/size]", "", $t); $regCouleur="[size= ?(:digit:+) ?]"; $t=ereg_replace($regCouleur, "", $t);

    // link $regLienSimple="[url] ?([^[]) ?[/url]"; $regLienEtendu="[url ?=([^[]) ?] ?([^]]*) ?[/url]"; if (ereg($regLienSimple, $t)) $t=ereg_replace($regLienSimple, "\1", $t); else $t=ereg_replace($regLienEtendu, "\1 (\2)", $t);

    // link (upper case tag) $regLienSimple="[URL] ?([^[]) ?[/URL]"; $regLienEtendu="[URL ?=([^[]) ?] ?([^]]*) ?[/URL]"; if (ereg($regLienSimple, $t)) $t=ereg_replace($regLienSimple, "\1", $t); else $t=ereg_replace($regLienEtendu, "\1 (\2)", $t);

    // mail $regMailSimple="[email] ?([^[]) ?[/email]"; $regMailEtendu="[email ?=([^[]) ?] ?([^]]*) ?[/email]"; if (ereg($regMailSimple, $t)) $t=ereg_replace($regMailSimple, "\1", $t); else $t=ereg_replace($regMailEtendu, "\1 (\2)", $t);

    // image $regImage="[img] ?([^[]) ?[/img]"; $regImageAlternatif="[img ?= ?([^[]) ?]"; if (ereg($regImage, $t)) $t=ereg_replace($regImage, "", $t); else $t=ereg_replace($regImageAlternatif, "", $t);

    // list $t=str_replace("[list]", "", $t); $t=str_replace("[/list]", "", $t); $t=str_replace("[]", " - ", $t); $t=str_replace("[/]", "\n", $t);

    // base URL $t=str_replace("base_url/", base_url(), $t);

    return $t; }

// ------------------------------------------------------------------------

/**

  • 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[] = '&lt;input type="button" class="button" id="'.$key.'" name="'.$key.'" value="'.$key.'" onClick="'.$val.'" /&gt;';
}

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; } ?>[/code]

And here is the config/bbcode.php file :

[code]<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/*
BBCODE
-------------------------------------------------------------------
This file contains an array of bbcode for use with the bbcode helper.

| */

$bbcode = array(

// name onClick

"b"            =>        "[removed]insert_bbcode('[b]', '[/b]');return(false)",
"i"            =>        "[removed]insert_bbcode('[i]', '[/i]');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('[url=]', '[/url]');return(false)",
"email"        =>        "[removed]insert_bbcode('[email=]', '[/email]');return(false)"
    );

?>[/code]

Clone this wiki locally