Solved

How to fix bug in bbcode .

Posted on 2012-03-29
9
232 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

943 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now