Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to fix bug in bbcode .

Posted on 2012-03-29
9
Medium Priority
?
244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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.

688 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