homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Preg Replace help

 2:40 am on Jun 29, 2010 (gmt 0)

Hi all,

I'm stuck on something involving preg_replace. I'm using it to create BBCode on my site. I have a code tag which I use to show how to use the BBCode, but the bbcode inside the code bbcode tags still renders as html.

The code is as follows:
$pattern[0] = "#\[code\](.+?)\[/code\]#is";
$pattern[1] = "#\[info\](.+?)\[/info\]#is";

$replacements[0] = '<pre class="code">$1</pre>';
$replacements[1] = '<div class="informationbox">$1</div>';

$p = preg_replace($pattern, $replacements, $post);

I think I have to change $pattern[1] so it will not change if it is between the code tags. Though I'm hoping it's possible to do that in the code regexp as i have more bbcode than shown here.

Does anyone know how I can about this?

Thanks in advance,



 6:14 pm on Jun 29, 2010 (gmt 0)

Might be best to convert all square brackets to their html entity counterpart inside the "escape" tags first, and remove the escape tags. Then run the main part of the BB code script, like so:
while(preg_match('/\[esc\](.*?)\[\/esc\]/im', $input, $out)) {
$repl = str_replace('[', '&#91;', $out[1]);
$repl = str_replace(']', '&#93;', $repl);
$input = str_replace($out[0], $repl, $input);
unset($repl, $out);
Note: I suggest using an escape tag, so people can still parse BB code inside of your code tags.
Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved