Building BBCode function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SadnesS
Forum Newbie
Posts: 5
Joined: Fri May 12, 2006 1:10 am

Building BBCode function

Post 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]
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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);
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
SadnesS
Forum Newbie
Posts: 5
Joined: Fri May 12, 2006 1:10 am

Post 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);
Post Reply