Need HELP with Regular Expression !!

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
New php-forum User
New php-forum User
Posts: 19
Joined: Wed Aug 17, 2005 6:00 am

Fri May 19, 2006 6:14 am

I'm so unfamiliar with regular expression, but i spend few days on it and come out with the following..

Code: Select all

function bbs_decode(&$message)
    $search = array ('/(\[img=)[\'"]?(.*?)[\'"]?]/',
    $replace = array ('<img src="$2">',
                      '<img src="$2">',
                      '<a href="http://$3" target="_blank">$4</a>',
                      '<span style="font-size: $2pt">$3</span>',
                      '<span style="color: $2">$3</span>',
                      '<span style="font-weight: bold">$1</span>',
                      '<span style="font-style: italic">$1</span>',
                      '<span style="font-style: underline">$1</span>',
                      '<div align="center"><table border=0 cellspacing=2 cellpadding=2 width="85%"><tr><td><font style=" font-weight:bold"> Quote from :  $2</font></td></tr><tr><td bgcolor="FFFFFF"  style="border-bottom:1px solid #9a94c5; border-top:1px solid #9a94c5; border-left:1px solid #9a94c5; border-right:1px solid #9a94c5">$3</td></tr></table></div>',
                      '<br />'
    echo nl2br(preg_replace($search,$replace,$message));
    return $message;
what i'm trying to do is to create a forum...
the above code works fine if the opening tag and the closing tag of the bbs code are in same line
for example

Code: Select all

 [color=red] red text [/color]
but if opening tag and closing tag are in different lines then it will not work

Code: Select all

red paragraph
second red line
i think include /n/r somewhere inside the regular expression ...
but... i really don't know how,
can anyone please help me to solve it !!
thanx in advance ~

User avatar
Posts: 1716
Joined: Sun Dec 29, 2002 5:03 am
Location: Portugal / Sweden

Mon May 22, 2006 4:15 am

Try removing the nl2br() function or perhaps put it inside you function and being something to be replaced too.
You can also simplify your code doing something like:

Code: Select all


function bbs_decode($message) {

$bb = array(
     '#\[h1\](.*?)\[/h1\]#si' => '<h1>\\1</h1>',
     '#\[h2\](.*?)\[/h2\]#si' => '<h2>\\1</h2>'

$message = preg_replace(array_keys($bb), array_values($bb), $message);
return $message;

Try the way this regex is done. Or something like (example):

Code: Select all

<?php '#([\t\r\n ])\[h1\](.*?)\[/h1\]#si' ?>
Gonçalo "gesf" Fontoura

Post Reply