PHP Preg_replace() multiple items

the master
06-28-2007, 12:49 AM
Hi, im using preg_replace() on my forums for the BB codes.

Example:
$Text=preg_replace("#<b>(.*?)</b>#si", "[ B]\\1[/ B]", $Text);

now if my post has something like "[ B]this is bold[ /B]" then i get "this is bold". the problem is that its possible to put "[ B][ B]this is bold[ /B][ /B]" which gives "[ B]this is bold[ /B]".

is there a way round this without some kind of loop? maby a parameter i need to set?

gazmac
07-18-2007, 06:30 AM
Hi, im using preg_replace() on my forums for the BB codes.

Example:
$Text=preg_replace("#<b>(.*?)</b>#si", "[ B]\\1[/ B]", $Text);

now if my post has something like "[ B]this is bold[ /B]" then i get "this is bold". the problem is that its possible to put "[ B][ B]this is bold[ /B][ /B]" which gives "[ B]this is bold[ /B]".

is there a way round this without some kind of loop? maby a parameter i need to set?

Personally, for something as simple as BB code, I would use str_replace, and make a simple function to replace the elements you wish to use, and remove the ones you wouldn't want to use. For a slightly more advanced version, you could try making a simple index (delimited text file) to refer to, as to easily update the BB code.

the master
07-18-2007, 02:24 PM
im using preg_replace because it will only replace certain patterns and avoids replacing incomplete BB tags

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum