Very simple bbcode

ApocDen 0 Tallied Votes 389 Views Share

This is a very simple bbcode example to use on a forum or your pages. Add this to a function page and make sure the function is required on the page you want to on.

ENJOY!! :D

<?php
  function bbcode($data)
  {
          $input = array(
                  '/\[b\](.*?)\[\/b\]/is',
                  '/\[i\](.*?)\[\/i\]/is',
                  '/\[u\](.*?)\[\/u\]/is',
                  '/\[img\](.*?)\[\/img\]/is',
                  '/\[url\](.*?)\[\/url\]/is',
                  '/\[url\=(.*?)\](.*?)\[\/url\]/is'
                  );

          $output = array(
                  '<strong>$1</strong>',
                  '<em>$1</em>',
                  '<u>$1</u>',
                  '<img src="$1" />',
                  '<a href="$1">$1</a>',
                  '<a href="$1">$2</a>'
                  );

          $rtrn = preg_replace ($input, $output, $data);

          return $rtrn;
  }
$bcode = $bbcode('[i]this is testing text[/i]');// This is how you use it on a forum or a page

echo $bcode;//output: <strong>this is again another test text</strong>
?>
Dani 4,312 The Queen of DaniWeb Administrator Featured Poster Premium Member

On line 26, I don't think you are supposed to have a $ when calling the bbcode() function.

tax14 0 Newbie Poster

As cccgal jas mentioned, replace the line

$bcode = $bbcode('[i]this is testing text[/i]');// This is how you use it on a forum or a page

with

$bcode = bbcode('[i]this is testing text[/i]');// This is how you use it on a forum or a page
phplover 0 Junior Poster in Training

Thanks for this :)

I was looking for an already made simple bbcode function. Will put this to good use.

Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.