I write a bbcode and I have a problem. I have this code:

function bbcode($tresc) 
  $tresc=str_replace("\n",'<br />',$tresc);
  return $tresc;  

I would like to in the tag [list] [/ list] - sign <br /> - turned to back to \n
I do not know how to do it. Please help.

Member Avatar


I modified a bit of code from the php manual (http://www.php.net/manual/fr/function.nl2br.php#100120):

$tresc = "[lista]hope\nhope\nancwjnc[/lista]";

function my_nl2br($list){
    if(preg_match_all('/\[lista\](.*?)\[\/lista\]/is', $list, $match)){
        foreach($match as $m){
            foreach($m as $guts){
            $list = str_replace('[lista]'.$guts.'[/lista]', "[lista]".str_replace("\n", "<br />", $guts)."[/lista]", $list);
return $list;

echo my_nl2br($tresc);