Jump to content

problem withh bbcode(preg_replace)


desithugg

Recommended Posts

[code]
<?
$bbcode = array(
"'Quote:'" => "[red]Quote:[/red]",
"'\[B\](.*?)\[/B\]'" => "<b>\\1</b>",
"'\[I\](.*?)\[/I\]'" => "<i>\\1</i>",
"'\[U\](.*?)\[/U\]'" => "<u>\\1</u>",
"'\[CODE\](.+?)\[/CODE\]'" => "<center><strong>Code:</strong><div align=\"center\" style=\"margin:0px 10px;padding:5px;border:1px dotted #000000;width:80%;\"></em>\\1</em></div></center>",
"'\[QUOTE\](.+?)\[/QUOTE\]'" => "<center><strong>Quote:</strong><div align=\"center\" style=\"margin:0px 10px;padding:5px;border:1px dotted #000000;width:80%;\"></em>\\1</em></div></center>",
"'\[URL=(.*?)\](.*?)\[/URL\]'" => "<a href=\"\\1\" target=\"_BLANK\">\\2</a>",
"'\[COLOR=(.*?)\](.*?)\[/COLOR\]'" => "<font color=\"\\1\">\\2</font>",
"'\[URL\](.*?)\[/URL\]'" => "<a href=\"\\1\" target=\"_BLANK\">\\1</a>",
"'\[IMG\](.*?)\[/IMG\]'" => "<img border=\"0\" src=\"\\1\">",
"'\[IMG=(.*?)\]'" => "<img border=\"0\" src=\"\\1\">",
"'\[RED\](.*?)\[/RED\]'" => "<font color=\"#FF6600\">\\1</font>",
"'\[b\](.*?)\[/b\]'" => "<b>\\1</b>",
"'\[i\](.*?)\[/i\]'" => "<i>\\1</i>",
"'\[u\](.*?)\[/u\]'" => "<u>\\1</u>",
"'\[quote\](.+?)\[/quote\]'" => "<center><strong>Quote:</strong><table width=\"80%\" class=\"mlinks2\"><tr><td width=\"100%\"></em>\\1</em></td></tr></table></center>",
"'\[code\](.+?)\[/code\]'" => "<center><strong>Code:</strong><table width=\"80%\" class=\"mlinks2\"><tr><td width=\"100%\"></em>\\1</em></td></tr></table></center>",
"'\[url=(.*?)\](.*?)\[/url\]'" => "<a href=\"\\1\" target=\"_BLANK\">\\2</a>",
"'\[color=(.*?)\](.*?)\[/color\]'" => "<font color=\"\\1\">\\2</font>",
"'\[url\](.*?)\[/url\]'" => "<a href=\"\\1\" target=\"_BLANK\">\\1</a>",
"'\[img\](.*?)\[/img\]'" => "<img border=\"0\" src=\"\\1\">",
"'\[img=(.*?)\]'" => "<img border=\"0\" src=\"\\1\">",
"'\[red\](.*?)\[/red\]'" => "<font color=\"#FF6600\">\\1</font>",
"':check:'" => " <img src=\"includes/smileys/check.gif\">",
"':angry:'" => " <img src=\"images/angry.gif\">",
"':biggrin:'" => "<img src=\"images/biggrin.gif\">",
"':blink:'" => " <img src=\"images/blink.gif\">",
"':cool:'" => " <img src=\"images/cool.gif\">",
"':dry:'" => "<img src=\"images/dry.gif\">",
"':huh:'" => "<img src=\"images/huh.gif\">",
"':laugh:'" => "<img src=\"images/laugh.gif\">",
"':ohmy:'" =>    "<img src=\"images/ohmy.gif\">",
"':rolleyes:'" =>    "<img src=\"images/rolleyes.gif\">",
"':ninja:'" => "<img src=\"images/ninja.gif\">",
"':\)'" => "<img src=\"images/smile.gif\">",
"':\('" => "<img src=\"images/sad.gif\">",
"':tongue:'" => "<img src=\"images/tongue.gif\">",
"':unsure:'" => "<img src=\"images/unsure.gif\">",
"'\[br\]'" => "<br>",
"'\[hr\]'" => "<hr align='center' width='80%' color='#FF6600'>",
);
$t_post = nl2br(preg_replace (array_keys($bbcode), array_values($bbcode), $row["post"]));
echo $t_post;
?>
[/code]

Evrything works fine but theres 1 problem...
if the post is somethig like
[colour=blue]my name[/colour] its shows [blue]"my name"[/blue] in the colour chosen
but if its spread over multiple lines like
[colour=blue]
my name
[/colour]
it just spits out the same thing back

sorry if im confusing but the problem is it only works if the tags are on 1 line if they are spread over multiple lines it just spits back out the same thing

if anyone gets what im trying to say is there any way I can get around the problem
Link to comment
https://forums.phpfreaks.com/topic/29942-problem-withh-bbcodepreg_replace/
Share on other sites

You need to add 's' to the end of each line. I couldn't find anything to explain what it exactly means in that usage, however "\s" is used to represent any whitespace character.

Also, you will probably want to add an "i" as well to make the replacements case insesitive so [ b ] == [ B ]

So the first line would be changed as follows:
[code]
"'\[B\](.*?)\[/B\]'" => "<b>\\1</b>",    --OLD LINE
"'\[B\](.*?)\[/B\]'is" => "<b>\\1</b>",  --NEW LINE
[/code]

Archived

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

×
×
  • Create New...

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.