SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    BBC Code Render issue

    When I enter the following bbc code (without periods)...
    Code:
    [.b]test[/b]
    [.i]test[/i]
    I getthe following...
    test <br/>
    test


    Here is my bbc to html php...
    Code:
    <?php
    function output_post ($post) {
    //Make safe any html
    $post_no_html = htmlspecialchars($post);
    
    $post_abridged = chop($post_no_html);
    $code_treated = preg_replace_callback(
     "/\[code\](.+?)\[\/code\]/s",
     create_function(
     '$matches',
     '$regex[0] = "[";
     $regex[1] = "]";
     $replace[0] = "[";
     $replace[1] = "]";
     ksort($regex);
     ksort($replace);
     $treated = str_replace($regex, $replace, $matches[1]);
     $output = "<table class=\"code\"><tr><td>Code:</td></tr><tr><td class=\"code_box\">$treated</td></tr></table>";
     return $output;'),
     $post_abridged);
            $bbcode_regex = array(0 => '/\[b\](.+?)\[\/b\]/s',
                                                    1 => '/\[i\](.+?)\[\/i\]/s',
                                                    2 => '/\[u\](.+?)\[\/u\]/s',
                                                    3 => '/\[quote\](.+?)\[\/quote\]/s',
                                                    4 => '/\[quote\=(.+?)](.+?)\[\/quote\]/s',
                                                    5 => '/\[url\](.+?)\[\/url\]/s',
                                                    6 => '/\[url\=(.+?)\](.+?)\[\/url\]/s',
                                                    7 => '/\[img\](.+?)\[\/img\]/s',
                                                    8 => '/\[img height\=(.+?)\](.+?)\[\/img\]/s',
                                                    9 => '/\[img width\=(.+?)\](.+?)\[\/img\]/s',
                                                    10 => '/\[img width\=(.+?) height\=(.+?)\](.+?)\[\/img\]/s',
                                                    11 => '/\[img height\=(.+?) width\=(.+?)\](.+?)\[\/img\]/s',
                                                    12 => '/\[color\=(.+?)\](.+?)\[\/color\]/s',
                                                    13 => '/\[size\=(.+?)\](.+?)\[\/size\]/s',
                                                    14 => '/\[tt\](.+?)\[\/tt\]/s',
                                                    15 => '/\[code\](.+?)\[\/code\]/s',
                                                    16 => '/\[sup\](.+?)\[\/sup\]/s',
                                                    17 => '/\[sub\](.+?)\[\/sub\]/s',
                                                    18 => '/\[list\](.+?)\[\/list\]/s',
                                                    19 => '/\[list\=(.+?)\](.+?)\[\/list\]/s',
                                                    20 => '/\[li\](.+?)\[\/li\]/s');
    
            $bbcode_replace = array(0 => '<b>$1</b>',
                                                    1 => '<i>$1</i>',
                                                    2 => '<u>$1</u>',
                                                    3 => '<table class="p_quote"><tr><td>Quote:</td></tr><tr><td class="p_quote_box">$1</td></tr></table>',
                                                    4 => '<table class="p_quote"><tr><td>$1 said:</td></tr><tr><td class="p_quote_box">$2</td></tr></table>',
                                                    5 => '<a href="$1">$1</a>',
                                                    6 => '<a href="$1">$2</a>',
                                                    7 => '<img src="$1" alt="User submitted image" title="User submitted image"/>',
                                                    8 => '<img height="$1" src="$2" alt="" />',
                                                    9 => '<img width="$1" src="$2" alt="" />',
                                                    10 => '<img width="$1" height="$2" src="$3" alt="" />',
                                                    11 => '<img height="$1" width="$2" src="$3" alt="" />',
                                                    12 => '<span style="color:$1">$2</span>',
                                                    13 => '<span style="font-size:$1">$2</span>',
                                                    14 => '<tt>$1</tt>',
                                                    15 => '<code>$1</code>',
                                                    16 => '<sup>$1</sup>',
                                                    17 => '<sub>$1</sub>',
                                                    18 => '<ul>$1</ul>',
                                                    19 => '<ul style="list-style-type:$1">$2</ul>',
                                                    20 => '<li>$1</li>');
    
            ksort($bbcode_regex);
            ksort($bbcode_replace);
    
            //preg_replace to convert all remaining bbCode tags
            $post_bbcode_treated = preg_replace($bbcode_regex, $bbcode_replace, $code_treated);
            $post_with_br = nl2br($post_bbcode_treated);
    
            echo $post_with_br;
    };
    ?>

  2. #2
    SitePoint Zealot agoossens's Avatar
    Join Date
    Mar 2004
    Location
    Adelaide, Australia
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, so what's the problem? You'll need to include some sort of problem statement and a question
    This space for rent.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    after it shows the bold text it writes out <br/> then returns and writes the italicized text. I don't know why it writes out <br/> and am not sure how to get rid of it.

  4. #4
    SitePoint Zealot agoossens's Avatar
    Join Date
    Mar 2004
    Location
    Adelaide, Australia
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I suggest you look up the nl2br function; I think you'll find it answers your question.
    This space for rent.

  5. #5
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I looked at that and I want to keep that.
    I found out the issue it is where i have..
    Code:
    $post_no_html = htmlspecialchars($post);
    i realize this prevents it from rendering html and it appears that it sees the new line as...
    Code:
    &lt;br/&rt;
    is there a way to make it see a new line as...
    Code:
    <br/>
    but not render other html

  6. #6
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use htmlspecialchars before you add html tags.

  7. #7
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i removed htmlspecialchars and nl2br and it seems to have fixed the issue. thanks for your help

  8. #8
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by HighCaliber
    i removed htmlspecialchars and nl2br and it seems to have fixed the issue. thanks for your help
    well, the purpose of a bbcode system is to allow people to use markup in thier text, without having to let them use html, because its dangerous to let people use html on your website.

    without htmlspecialchars, you have defeated much of the purpose of a bbcode system. users can now freely inject html.

    even with it, people could still inject some javascript and html attributes into your current system. its not very good.

    properly creating a bbcode system is not as easy as many people think it is. a lot of tutorials provide bad methods. if you dont do it right, you may not be much better off, and in some cases, worse off.

    pear has a nice package for bbcode. im sure theres others as well.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •