Jump to content

bbcode help


extrovertive

Recommended Posts

Below is a simple class that replaces [  b] whatever [ /b ] to [b]whatever[/b]

Why doesn't it work?
[code=php:0]
class BBCode
{
public $inputString;
public $outputString;
 
  function __construct() {}
  function __deconstruct() {}
 
  function BBParse ($input)
  {
      $this->inputString = $input;
     
      if(strlen($this->inputString) < 20)
      {
      echo 'You have to provide at least a message of 20 characters!';
      }
      else
      {
          // Define some default BB code tags, such as bold, italic, and url.
          $BBCode[0] = '/[b]/';
          $BBCode[1] = '/[/b]/';
          $BBCode[2] = '/[i]/';
          $BBCode[3] = '/[/i]/';
          $BBCode[4] = '/[url=http://www.phpfreaks.com/';
          $BBCode[5] = '/]/';
          $BBCode[5] = '/[/url]/';
         
          // Replacement strings, in HTML ofcourse.
          $BBReplace[0] = '<b>';
          $BBReplace[1] = '</b>';
          $BBReplace[2] = '<i>';
          $BBReplace[3] = '</i>';
          $BBReplace[4] = '<a href=' . $this->inputString . '>';
          $BBReplace[5] = '</a>';
         
      $this->outputString = preg_replace($BBCode, $BBReplace, $this->inputString);
      return $this->outputString;
      }
  }
}

$message = "[b]well[/b]! You [i]suck[/i]!";
$bbcode = new BBCode($message);

echo $bbcode->BBParse($message);
[/code]
Warning: preg_replace() [function.preg-replace]: Unknown modifier 'b' in C:\wamp\www\Source\Classes\~phpdesigner_outputlocal_tmp~6694.php on line 39

Warning: preg_replace() [function.preg-replace]: Unknown modifier ']' in C:\wamp\www\Source\Classes\~phpdesigner_outputlocal_tmp~6694.php on line 39

Warning: preg_replace() [function.preg-replace]: Unknown modifier 'r' in C:\wamp\www\Source\Classes\~phpdesigner_outputlocal_tmp~6694.php on line 39
[]we[/]! Yo []sck[/]!
Link to comment
Share on other sites

You need to escape the square brackets like so:

[b][nobbc]/\[b\]/[/nobbc][/b]

Also Id recommend you to do your BBCode in pairs, so the regular expressions will look like this:
[b]/\[b\](.*)\[\/b\]/is[/b]
Notice the i and the s at the end of the regular express this tells the PCRE Engine to be case-insensitive and to ignore newlines/carriage returns. So if you did this:
[nobbc][b]something

something else here

and agaig[/b][/nobbc]
It'll parse it, whereas if you didnt have the s modifier it will stop once it gets to the newline. and wont parse the string.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.