Solved

How to fix bug in bbcode .

Posted on 2012-03-29
9
236 Views
Last Modified: 2012-04-18
Hello

i make code my forums, i use bbcode and found bug <br />
how to not add <br / > in

Open in new window


        function bbcode_format ($str) {  
           // $str = htmlentities($str);  ¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿
              $str = htmlspecialchars($str);
          
            $simple_search = array(  
                        //added line break  
                        '/\[br\]/is',  
                        '/\[b\](.*?)\[\/b\]/is',  
                        '/\[i\](.*?)\[\/i\]/is',  
                        '/\[u\](.*?)\[\/u\]/is',  
                        '/\[url\=(.*?)\](.*?)\[\/url\]/is',  
                        '/\[url\](.*?)\[\/url\]/is',  
                        '/\[align\=(left|center|right)\](.*?)\[\/align\]/is',  
                        '/\[img\](.*?)\[\/img\]/is',  
                        '/\[mail\=(.*?)\](.*?)\[\/mail\]/is',  
                        '/\[mail\](.*?)\[\/mail\]/is',  
                        '/\[font\=(.*?)\](.*?)\[\/font\]/is',  
                        '/\[size\=(.*?)\](.*?)\[\/size\]/is',  
                        '/\[color\=(.*?)\](.*?)\[\/color\]/is',  
                          //added textarea for code presentation  
                       '/\[codearea\](.*?)\[\/codearea\]/is',  
                         //added pre class for code presentation  
                      '/\[code\](.*?)\[\/code\]/is',  
                        //added paragraph  
                      '/\[p\](.*?)\[\/p\]/is',  
                        );  
          
            $simple_replace = array(  
                        //added line break  
                       '<br />',  
                        '<strong>$1</strong>',  
                        '<em>$1</em>',  
                        '<u>$1</u>',  
                        // added nofollow to prevent spam  
                        '<a href="$1" rel="nofollow" title="$2 - $1"  target="_blank">$2</a>',  
                        '<a href="$1" rel="nofollow" title="$1"  target="_blank">$1</a>',  
                        '<div style="text-align: $1;">$2</div>',  
                        //added alt attribute for validation  
                        '<a href="$1" rel="nofollow" target="_blank"><img  class="thumb" src="$1" alt="http://www.ai4investment.com" /></a>',  
                        '<a href="mailto:$1"  target="_blank">$2</a>',  
                        '<a href="mailto:$1"  target="_blank">$1</a>',  
                        '<span style="font-family: $1;">$2</span>',  
                        '<span style="font-size: $1;">$2</span>',  
                        '<span style="color: $1;">$2</span>',  
                        //added textarea for code presentation  
                        '<textarea class="code_container" rows="30" cols="70">$1</textarea>',  
                        //added pre class for code presentation  
                        '<pre class="brush: cpp">$1</pre>',  
                        //added paragraph  
                        '<p>$1</p>',  
                        );  
          
            // Do simple BBCode's  
            $str = preg_replace ($simple_search, $simple_replace, $str);  
          
            // Do <blockquote> BBCode  
            $str = $this->bbcode_quote ($str);  
            $bbcode = nl2br($str);
          
            return $bbcode;  
        }     

Open in new window

0
Comment
Question by:XSoFTz
  • 5
  • 4
9 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 37783445
> ... how to not add <br / > 
remove first array element in both arrays
0
 

Author Comment

by:XSoFTz
ID: 37785179
i mean

$bbcode = nl2br($str);

Open in new window


but i need add nl2br then check in bbcode [ code ][ /code ] and not add nl2br ?
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 37785864
hmm, don't know what nl2br() does, assuming that it converts 0x0a to <br > then according your question:
> ... how to not add <br / > 
simply do not call it

may be I don't understand what you want to archive, but your description is very vage ...
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:XSoFTz
ID: 37785888
about code it work but.

in tag code

[ code ]
if()
{

}
[ /code ]

PHP Code Line :

$bbcode = nl2br($str);

Output :

[ code ]
if()<br />
{<br />
<br />
}<br />
[ /code ]

it add <br /> in code. but i not need <br /> add in tag code

then not use nl2br() function, in tag \n not work display on html code.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 37785928
you need to parse your code and exclude those parts which you define as code from being passed to nl2br()

You need a good parser for that as it cannot simply be done with regex, see this example

[ code ]
if(x=="[ /code ]"){
  bang="\
[ code ]  \
";
/*
[ /code ]
*/
hidden=true;
}
[ /code ]
0
 

Author Comment

by:XSoFTz
ID: 37786034
not so it .

see

DisplayHtml Code
i not get <br /> in tag [ code ]

in bbcode :

[ code ]
int start() {
   if (gs_108 != gs_100) return (0);
   int ind_counted_0 = IndicatorCounted();
   int li_8 = Bars - ind_counted_0;
   if (li_8 > 1) li_8 = Bars - 3;
   double ld_12 = p / 100.0;
   for (int li_4 = li_8; li_4 >= 0; li_4--) {
      if (Close[li_4] < gd_132) {
         gd_132 = Close[li_4];
         gd_148 = gd_132 * (2.0 * ld_12 + 1.0);
         gd_140 = gd_132 * (ld_12 + 1.0);
      }
      if (Close[li_4] > gd_148) {
         gd_148 = Close[li_4];
         gd_132 = gd_148 * (1 - 2.0 * ld_12);
         gd_140 = gd_148 * (1 - ld_12);
      }
      g_ibuf_120[li_4] = gd_148;
      g_ibuf_124[li_4] = gd_140;
      g_ibuf_128[li_4] = gd_132;
   }
   return (0);
}
[ /code ]
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 37786331
> i not get <br /> in tag [ code ]
I'm confused, what exactly is your problem then?
can you please give an example verbatime what you have, what you expect and what you get (which is buggy in your opinion)
0
 

Accepted Solution

by:
XSoFTz earned 0 total points
ID: 37786368
now i need fix bug..

1. put bbcode in my code
[ code ]
int start() {
   if (gs_108 != gs_100) return (0);
   int ind_counted_0 = IndicatorCounted();
   int li_8 = Bars - ind_counted_0;
   if (li_8 > 1) li_8 = Bars - 3;
   double ld_12 = p / 100.0;
   for (int li_4 = li_8; li_4 >= 0; li_4--) {
      if (Close[li_4] < gd_132) {
         gd_132 = Close[li_4];
         gd_148 = gd_132 * (2.0 * ld_12 + 1.0);
         gd_140 = gd_132 * (ld_12 + 1.0);
      }
      if (Close[li_4] > gd_148) {
         gd_148 = Close[li_4];
         gd_132 = gd_148 * (1 - 2.0 * ld_12);
         gd_140 = gd_148 * (1 - ld_12);
      }
      g_ibuf_120[li_4] = gd_148;
      g_ibuf_124[li_4] = gd_140;
      g_ibuf_128[li_4] = gd_132;
   }
   return (0);
}
[ /code ]

Open in new window



2. i use function

function bbcode_format ($str) {  
           // $str = htmlentities($str);  ¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿
              $str = htmlspecialchars($str);
          
            $simple_search = array(  
                        //added line break  
                        '/\[br\]/is',  
                        '/\[b\](.*?)\[\/b\]/is',  
                        '/\[i\](.*?)\[\/i\]/is',  
                        '/\[u\](.*?)\[\/u\]/is',  
                        '/\[url\=(.*?)\](.*?)\[\/url\]/is',  
                        '/\[url\](.*?)\[\/url\]/is',  
                        '/\[align\=(left|center|right)\](.*?)\[\/align\]/is',  
                        '/\[img\](.*?)\[\/img\]/is',  
                        '/\[mail\=(.*?)\](.*?)\[\/mail\]/is',  
                        '/\[mail\](.*?)\[\/mail\]/is',  
                        '/\[font\=(.*?)\](.*?)\[\/font\]/is',  
                        '/\[size\=(.*?)\](.*?)\[\/size\]/is',  
                        '/\[color\=(.*?)\](.*?)\[\/color\]/is',  
                          //added textarea for code presentation  
                       '/\[codearea\](.*?)\[\/codearea\]/is',  
                         //added pre class for code presentation  
                      '/\[code\](.*?)\[\/code\]/is',  
                        //added paragraph  
                      '/\[p\](.*?)\[\/p\]/is',  
                        );  
          
            $simple_replace = array(  
                        //added line break  
                       '<br />',  
                        '<strong>$1</strong>',  
                        '<em>$1</em>',  
                        '<u>$1</u>',  
                        // added nofollow to prevent spam  
                        '<a href="$1" rel="nofollow" title="$2 - $1"  target="_blank">$2</a>',  
                        '<a href="$1" rel="nofollow" title="$1"  target="_blank">$1</a>',  
                        '<div style="text-align: $1;">$2</div>',  
                        //added alt attribute for validation  
                        '<a href="$1" rel="nofollow" target="_blank"><img  class="thumb" src="$1" alt="http://www.ai4investment.com" /></a>',  
                        '<a href="mailto:$1"  target="_blank">$2</a>',  
                        '<a href="mailto:$1"  target="_blank">$1</a>',  
                        '<span style="font-family: $1;">$2</span>',  
                        '<span style="font-size: $1;">$2</span>',  
                        '<span style="color: $1;">$2</span>',  
                        //added textarea for code presentation  
                        '<textarea class="code_container" rows="30" cols="70">$1</textarea>',  
                        //added pre class for code presentation  
                        '<pre class="brush: cpp">$1</pre>',  
                        //added paragraph  
                        '<p>$1</p>',  
                        );  
          
            // Do simple BBCode's  
            $str = preg_replace ($simple_search, $simple_replace, $str);  
          
            // Do <blockquote> BBCode  
            $str = $this->bbcode_quote ($str);  
         
            return $bbcode;  
        }     

Open in new window


3. in display text in forums it ok, but text not convert \n to <br />

4. i insert code in function.
            $bbcode = nl2br($str);

Open in new window


5. but display in forums in tag [ code ] and show <br />, i not need it
Not work
6. i need u help fix or find function for work bbcode...
0
 

Author Closing Comment

by:XSoFTz
ID: 37859812
....
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question