Page 1 of 1

Building BBCode function

Posted: Thu May 18, 2006 3:21 am
by SadnesS
Hello,

i wanted to build to my site BBCode functions that my users can use.

Here is the bbcode.php

Code: Select all

<?php
function bbcode($content){ //function starts
$content = nl2br(htmlspecialchars($content)); //writed message

    $bbcode = array(
       "'\[b\](.*?)\[/b\]'", // Bold
       "'\[i\](.*?)\[/i\]'", // Italics
       "'\[u\](.*?)\[/u\]'", // Underline
       "'\[img\](.*?)\[/img\]'" // Image
        );

      $html = array( // HTML
        "<b>\\1</b>", // Bold
        "<i>\\1</i>", // Italics
        "<u>\\1</u>", // Underlined
        "<img border=\"0\" src=\"\\1\">" // image
       );

      $content = preg_replace($bbcode, $html, $content); // replaces all BBCode tags with HTML
    return nl2br($content);
}

?>
And here is example to use it:

Code: Select all

$text = bbcode($text):
And i have used this to get rid of those <br />:

Code: Select all

$textt = str_replace(array("<br />","<br />","<br />"), array("\n","\r","\r\n"), $text);
Everything almost works fine, but something adds too many <br /> code to text, when i used it, example
This is test. Something something.

blaah blaah blaah blaah

blaah
And when you edit text and upload it again to database, it looks like this:
This is test. Something something.



blaah blaah blaah blaah



blaah
And code for text looks like this:

Code: Select all

This is test. Something something.<br /><br />
<br /><br />
blaah blaah blaah blaah<br /><br /><br /><br />
blaah
I have no idea, what is problem here.

Hope you understand the problem and sorry my bad examples and bad code![/quote]

Posted: Thu May 18, 2006 3:26 am
by onion2k

Code: Select all

$textt = str_replace(array("<br />","<br />","<br />"), array("\n","\r","\r\n"), $text);
That will replace the carriage returns in the order of the array.. so..

Hello\r\nOnion

..will become..

Hello\r\nOnion
Hello\r<br \>Onion
Hello<br \><br \>Onion

Your code should be

Code: Select all

$textt = str_replace(array("<br />","<br />","<br />"), array("\r\n","\n","\r"), $text);
EDIT: Actually.. I'm not sure that's right. It might be replacing them all seperately.. so you'd need to make two passes:

Code: Select all

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

Posted: Thu May 18, 2006 7:18 am
by Chris Corbyn
nl2br() does *not* remove the EOL character and replace it with <br />. It simply add a <br /> before the EOL. So you should not be replacing <br /> with \n, you should just be removing it altogether.

Posted: Sun May 21, 2006 3:22 am
by SadnesS
d11wtq wrote:nl2br() does *not* remove the EOL character and replace it with <br />. It simply add a <br /> before the EOL. So you should not be replacing <br /> with \n, you should just be removing it altogether.
Hmm, how i can do that?

Sorry, i'm not pro with PHP but i want learn it.

Wuhuu, i figured it out myself. Here is the correct code:

Code: Select all

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