Jump to content

Recommended Posts

Ok, so i'm working on my forum software in PHP to both get some knowledge out of this and accomplish something in the run. While adding a BBCode parser, I ran into some trouble.

 

This is what happened:

http://ebulletin.ballindesign.com/message.php?id=1

 

As you can see, a <br /> tag shows up after EVERY line...

Here is my BBCode parser:

 

<?php 

function is_odd($intNumber) 
{ 
  if ($intNumber % 2 == 0 ) return true; 
  else return false; 
} 
function badlink($link, $prefix) { 
    if ($prefix == "mailto:") { 
        if (strpos($link, "@") === FALSE || strpos($link, ".", (strpos($link, "@")+2)) === FALSE || substr_count($link, "@") > 1 || strpos($link, "@") == 0) { 
            return 1; 
            } 
        } 
    if (strpos($link, ".") == 0 || strpos($link, ".") == strlen($link) || (strpos($link, "/") < strpos($link, ".") && strpos($link, "/") !== FALSE)) { 
        return 1;  
        } 
    }; 
function setlinks($r, $prefix) { 
    if (substr($r, 0, strlen($prefix)) == $prefix) { 
        $r = "\n".$r; 
        }
    $r = str_replace("<br>".$prefix, "<br>\n".$prefix, $r); 
    $r = str_replace(" ".$prefix, " \n".$prefix, $r); 
    while (strpos($r, "\n".$prefix) !== FALSE) { 
        list($r1, $r2) = explode("\n".$prefix, $r, 2); 
        if (strpos($r2, " ") === FALSE && strpos($r2, "<br>") === FALSE) { 
            if ($prefix != "mailto:") { 
                $target = ' target="_blank"'; 
                } 
            else { 
                $target = ""; 
                } 
            if (strpos($r2, ".") > 1 && strpos($r2, ".") < strlen($r2) && badlink($r2, $prefix) != 1) { 
                $r = $r1.'<a href="'.$prefix.$r2.'"'.$target.'><font size="2" color="blue">'.$prefix.$r2.'</font></a>'; 
                } 
            else { 
                $r = $r1.$prefix.$r2; 
                } 
            } 
        else { 
            if (strpos($r2, " ") === FALSE || ( strpos($r2, " ") > strpos($r2, "<br>") && strpos($r2, "<br>") !== FALSE)) { 
                list($r2, $r3) = explode("<br>", $r2, 2); 
                if (badlink($r2, $prefix) != 1) { 
                    $r = $r1.'<a href="'.$prefix.$r2.'"'.$target.'><font size="3" color="blue">'.$prefix.$r2.'</font></a><br>'.$r3; 
                    } 
                else { 
                    $r = $r1.$prefix.$r2.'<br>'.$r3; 
                    } 
                } 
            else { 
                list($r2, $r3) = explode(" ", $r2, 2); 
                if (strpos($r2, ".") > 1 && strpos($r2, ".") < strlen($r2) && badlink($r2, $prefix) != 1) { 
                    $r = $r1.'<a href="'.$prefix.$r2.'"'.$target.'><font size="3" color="blue">'.$prefix.$r2.'</font></a> '.$r3; 
                    } 
                else { 
                    $r = $r1.$prefix.$r2.' '.$r3; 
                    } 
                } 
            } 
        } 
    return $r; 
    }; 


function BBCode($r) 
{ 

$r = trim($r); 
$r = htmlentities($r);
$r = str_replace("\r\n","<br>",$r); 
$r = str_replace("\r\n","</br>",$r); 
$r = str_replace("[b]","<b>",$r); 
$r = str_replace("[/b]","</b>",$r); 
$r = str_replace("[img]http://","<img src='",$r); 
$r = str_replace("[/img]","'>",$r); 
$r = str_replace("[img]http://","<img src='",$r); 
$r = str_replace("[/img]","'>",$r); 
$r = str_replace("[s]","<s>",$r); 
$r = str_replace("[/s]","</s>",$r); 
$r = str_replace("[ul]","<ul>",$r); 
$r = str_replace("[/ul]","</ul>",$r); 
$r = str_replace("[list][*]","<li>",$r); 
$r = str_replace("[/list]","</li>",$r); 
$r = str_replace("[ol]","<ol>",$r); 
$r = str_replace("[/ol]","</ol>",$r); 
$r = str_replace("[quote]","<br /><table width='80%' bgcolor='#ffff66' align='center'><tr><td style='border: 1px dotted black'><font color=black><b>Quote:<br></b>",$r); 
$r = str_replace("[/quote]","</font></td></tr></table>",$r); 
$r = str_replace("[i]","<i>",$r); 
$r = str_replace("[/i]","</i>",$r); 
$r = str_replace("[u]","<u>",$r); 
$r = str_replace("[/u]","</u>",$r); 
$r = str_replace("
[spoiler]",'[spoiler]<font bgcolor ="#000000" color="#DDDDDD">',$r); 
$r = str_replace("[/spoiler]
","</font>[/spoiler]",$r); 

//set [link]s 
while (strpos($r, "[link=") !== FALSE) 
{ 
    list ($r1, $r2) = explode("[link=", $r, 2); 
    if (strpos($r2, "]") !== FALSE) { 
        list ($r2, $r3) = explode("]", $r2, 2); 
        if (strpos($r3, "[/link]") !== FALSE) { 
            list($r3, $r4) = explode("[/link]", $r3, 2); 
            $target = ' target="_blank"'; 
            if (substr($r2, 0, 7) == "mailto:") { 
                $target = ""; 
            } 
            $r = $r1.'<a href="'.$r2.'"'.$target.'><font size="3" color="blue">'.$r3.'</font></a>'.$r4; 
        } 
        else { 
            $r = $r1."[link\n=".$r2."]".$r3; 
        } 
    } 
    else { 
        $r = $r1."[link\n=".$r2; 
    } 
} 
$r = str_replace("[link\n=","[link=",$r); 
////[link] 

///default url link setting 
$r = setlinks($r, "http://"); 
$r = setlinks($r, "https://"); 
$r = setlinks($r, "ftp://"); 
$r = setlinks($r, "mailto:"); 
////links 

///emoticons 
$r = str_replace("",'<img src="images/smilie.gif">',$r); 
$r = str_replace("",'<img src="images/sad.gif">',$r); 
$r = str_replace("",'<img src="images/angry.gif">',$r); 
$r = str_replace("",'<img src="images/biggrin.gif">',$r); 
$r = str_replace("",'<img src="images/blink.gif">',$r); 
$r = str_replace(":blush:",'<img src="images/blush.gif">',$r); 
$r = str_replace("B)",'<img src="images/cool.gif">',$r); 
$r = str_replace("",'<img src="images/dry.gif">',$r); 
$r = str_replace("",'<img src="images/happy.gif">',$r); 
$r = str_replace("",'<img src="images/confused.gif">',$r); 
$r = str_replace("",'<img src="images/laugh.gif">',$r); 
$r = str_replace("",'<img src="images/ohmy.gif">',$r); 
$r = str_replace(":fear:",'<img src="images/fear.gif">',$r); 
$r = str_replace("",'<img src="images/rolleyes.gif">',$r); 
$r = str_replace(":sleep:",'<img src="images/sleep.gif">',$r); 
$r = str_replace("",'<img src="images/tongue.gif">',$r); 
$r = str_replace("",'<img src="images/tongue.gif">',$r); 
$r = str_replace("",'<img src="images/unsure.gif">',$r); 
$r = str_replace("",'<img src="images/wacko.gif">',$r); 
$r = str_replace(":wink:",'<img src="images/wink.gif">',$r); 
$r = str_replace("",'<img src="images/wub.gif">',$r); 

$r = trim($r); 
return $r; 

} 

?>

 

Link to comment
https://forums.phpfreaks.com/topic/212130-bbcode-parser-mess-up/
Share on other sites

I'm a little confused by lines 70 and 71:

 

$r = str_replace("\r\n","<br>",$r); 
$r = str_replace("\r\n","</br>",$r);

 

Since </br> is not valid HTML, I'm not sure how browsers would react to that one way or the other. But surely you do not mean to replace each return with <br></br>.

 

Interestingly, if you notice in the HTML output the extraneous <br />'s that are being included are being included with their brackets as HTML entities, which is why they're showing up. Yet your only reference to this self-closing tag format is on line 81:

 

$r = str_replace("[quote]","<br /><table width='80%' bgcolor='#ffff66' align='center'><tr><td style='border: 1px dotted black'><font color=black><b>Quote:<br></b>",$r); 

 

Is there more code involved in the process of generating that HTML?

What's awry here, is that the code showing up on that page is a <br /> tag and you only have a <br /> self-closing format style tag in one place in the code you posted, and it's coupled with other HTML that is not iterating alongside the ones you see on that page, meaning that (line 81) is not the source.

 

Somehow, the source of the <br /> tags you are seeing have not been posted in your example code so far. It simply has to be coming from somewhere else, somehow. To prove this, I encourage you to change all of the line break tags in your code to non-self closing format <br> instead of <br /> on line 81. Also, get rid of the line that says </br> because this is not valid HTML and is adding unnecessary variables to the equation. I am 95% certain when you change all break tags in the code you posted to <br> format, the mysterious <br /> will continue to show, proving that it is originating from elsewhere.

What's awry here, is that the code showing up on that page is a <br /> tag and you only have a <br /> self-closing format style tag in one place in the code you posted, and it's coupled with other HTML that is not iterating alongside the ones you see on that page, meaning that (line 81) is not the source.

 

Somehow, the source of the <br /> tags you are seeing have not been posted in your example code so far. It simply has to be coming from somewhere else, somehow. To prove this, I encourage you to change all of the line break tags in your code to non-self closing format <br> instead of <br /> on line 81. Also, get rid of the line that says </br> because this is not valid HTML and is adding unnecessary variables to the equation. I am 95% certain when you change all break tags in the code you posted to <br> format, the mysterious <br /> will continue to show, proving that it is originating from elsewhere.

 

 

errm, I really don't understand what you're telling me to do...

what you should do then  is call nl2br() after your have called bbcode(). Now remove these lines

$r = str_replace("\r\n","<br>",$r); 
$r = str_replace("\r\n","</br>",$r);

From your function, they are not needed. nl2br convert newlines to <br /> for you so that above lines are not needed.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.