Function to convert BBcode in HTML tags

Place for comments, problems, questions, or any issue related to the JavaScript / PHP scripts from this site.
User avatar
Posts: 150
Location: Holland Rotterdam

Function to convert BBcode in HTML tags

Best Admin first off all how are you doing...
Recently I`m busy to improve my comment-system.

But Function to convert BBcode in HTML tags
I can`t get it to work it does not convert.

I have my script in PDO

Code: Select all

   $output .= '
   <div class="panel panel-default" style="margin-left:'.$marginleft.'px">
    <div class="panel-heading"><img class="img-circle" src="profile-image/'. $row['comment_sender_id']. '.jpg" width="50" height="50"></img class>By <b>'.$row["comment_sender_name"].'</b>Comment to: '.$row["reply_to"].'<br> on <i>'.$row["date"].'</i></div>
    <div class="panel-body">'.$row["comment"].'</div>
This is the script.php from your script

Code: Select all

// Function to convert BBcode in HTML tags ( )
function formatBBcode($str) {
  // characters that represents bbcode, and smiles
  $bbcode = array(
  '/\[b\](.*?)\[\/b\]/is', '/\[i\](.*?)\[\/i\]/is', '/\[u\](.*?)\[\/u\]/is',      // for format text
  '/\[url\=(.*?)\](.*?)\[\/url\]/is',                           // for URL
  '/\[color\=#([0-9a-f]{3,6})\](.*?)\[\/color\]/is',            // for color
  // smiles
  '/:\)/i', '/:\(/i', '/:P/i', '/:D/i', '/:S/i', '/:O/i', '/:=\)/i', '/:\|H/i', '/:X/i', '/:\-\*/i');

  // HTML code that replace bbcode, and smiles characters
  $htmlcode = array(
  '<b>$1</b>', '<i>$1</i>', '<u>$1</u>',                   // format text
  '<a target="_blank" href="$1" title="$2">$2</a>',        // URL
  '<span style="color:#$1;">$2</span>',                    // color
  // smiles (from "icos/" directory)
  '<img src="icos/0.gif" alt=":)" style="border:none;" />',
  '<img src="icos/1.gif" alt=":(" style="border:none;" />',
  '<img src="icos/2.gif" alt=":P" style="border:none;" />',
  '<img src="icos/3.gif" alt=":D" style="border:none;" />',
  '<img src="icos/4.gif" alt=":S" style="border:none;" />',
  '<img src="icos/5.gif" alt=":O" style="border:none;" />',
  '<img src="icos/6.gif" alt=":=)" style="border:none;" />',
  '<img src="icos/7.gif" alt=":|H" style="border:none;" />',
  '<img src="icos/8.gif" alt=":X" style="border:none;" />',
  '<img src="icos/9.gif" alt=":-*" style="border:none;" />'

  $str = preg_replace($bbcode, $htmlcode, $str);   // perform replaceament

  return nl2br($str);      // nl2br() to replace new line characters with <br>

// if data from POST "txtmsg", gets the text
// delete tags, replace BBcode with HTML, and output the text
$str = isset($_POST['txtmsg']) ? formatBBcode( strip_tags($_POST['txtmsg'], '<b><i><u><span><img><a>') ) : '';
echo $str;

I just don`t have any clu how to place this part from the code into '.$row["comment"].'

Code: Select all

$str = isset($_POST['txtmsg']) ? formatBBcode( strip_tags($_POST['txtmsg'], '<b><i><u><span><img><a>') ) : '';
echo $str;

I'm fine, thanks.

The formatBBcode() function converts bbcode tags from a string (such as: [b]text[/b] and [i]text[/i]) into html tags (<b>text</b> and <i>text</i>); and it receives a single argument: $str - the text which contains bbcode tags.

- In your mysql table add the content (comment) with bbcode tags, then use the:
when you want to output /show the comment.

Similar Topics