Jump to content


Photo

bbcode help


  • Please log in to reply
1 reply to this topic

#1 extrovertive

extrovertive
  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 24 August 2006 - 09:57 AM

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

Why doesn't it work?
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);
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[/]!

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 24 August 2006 - 01:47 PM

You need to escape the square brackets like so:

/\[b\]/

Also Id recommend you to do your BBCode in pairs, so the regular expressions will look like this:
/\[b\](.*)\[\/b\]/is
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:
[b]something

something else here

and agaig[/b]
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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users