Jump to content

Archived

This topic is now archived and is closed to further replies.

extrovertive

bbcode help

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[/]!

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

×

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.