extrovertive Posted August 24, 2006 Share Posted August 24, 2006 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 39Warning: preg_replace() [function.preg-replace]: Unknown modifier ']' in C:\wamp\www\Source\Classes\~phpdesigner_outputlocal_tmp~6694.php on line 39Warning: 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[/]! Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted August 24, 2006 Share Posted August 24, 2006 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]somethingsomething else hereand 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.