Web Analytics Made Easy -
StatCounter restrict nl2br to outside BBCode tags only - CodingForum

Announcement

Collapse
No announcement yet.

restrict nl2br to outside BBCode tags only

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • restrict nl2br to outside BBCode tags only

    We are running MyBB forum in which adds newlines via class_parse.php
    https://crossreference.mybb.de/nav.h...hp.source.html

    We are trying to get a syntax highlighter to work for python, however the codebox has <br> in it from the nl2br() function

    Click image for larger version

Name:	syntax.png
Views:	1
Size:	13.5 KB
ID:	2283643
    This is our current output. However if we simply comment out the function nl2br() it removes ALL breaklines and the entire post becomes unformatted. We would like to modify this so we can only do nl2br() outside of a certain BBCode

    We are using pre tags to insert the codebox via BBCode
    Code:
    [pre class="brush: python"]
    print('hello world')
    
    print("test")
    [/pre]
    which gets turned into html via regex
    Code:
    <pre class="brush: python">$1</pre>
    Is there anyway to restrict nl2br() wtihin these pre tags so that the line breaks do not show in codeboxes?

    EDIT:
    this is the specific function within class_parser.php that converts newline to breaks wtihin the post bit
    Code:
     if(!isset($this->options['nl2br']) || $this->options['nl2br'] != 0)
            {
                $message = nl2br($message);
                // Fix up new lines and block level elements
                $message = preg_replace("#(</?(?:html|head|body|div|p|form|table|thead|tbody|tfoot|tr|td|th|ul|ol|li|div|p|blockquote|cite|hr)[^>]*>)\s*  <br />#i", "$1", $message);
                $message = preg_replace("#(&nbsp;)+(</?(?:html|head|body|div|p|form|table|thead|tbody|tfoot|tr|td|th|ul|ol|li|div|p|blockquote|cite|hr)[^>]*>)#i", "$2", $message);
            }
    EDIT:
    I tried adding this function
    Code:
    function my_nl2br($string){
    $string = str_replace("\n", "<br />", $string);
    if(preg_match_all('/\<pre\>(.*?)\<\/pre\>/', $string, $match)){
        foreach($match as $a){
            foreach($a as $b){
            $string = str_replace('<pre>'.$b.'</pre>', "<pre>".str_replace("<br />", "", $b)."</pre>",       $string);
            } 
        }
    }   
    return $string;
    }
    and switching it to use my_nl2br instead of nl2br, and the posts looks good, but it no longer breaks the line within the codebox, and keeps the <br> that it adds to the codebox?
    Click image for larger version

Name:	test.jpg
Views:	1
Size:	44.8 KB
ID:	2283644
    Last edited by metulburr; Oct 4, 2016, 12:12 AM.

  • #2
    Generally speaking, welcome to why I usually don't even bother trying to make that conversion anymore unless it REALLY matters, and instead just use "white-space: pre-wrap;" in the CSS client-side.

    Doesn't work in IE7/earlier, OH WELL.

    pre-wrap is nice because it will also obey a first-line indent on \n.

    Increasingly CSS is lightening our workload both client and server-side. Great, isn't it?
    Walk the dark path, sleep with angels, call the past for help.
    https://cutcodedown.com
    https://medium.com/@deathshadow

    Comment


    • #3
      I fixed it by using this instead
      Code:
      function my2_nl2br($string){
          $string = str_replace("\n", "<br />", $string);
          if(preg_match_all('/\<pre class="brush: python"\>(.*?)\<\/pre\>/', $string, $match)){
              foreach($match as $a){
                  foreach($a as $b){
                      $string = str_replace('<pre class="brush: python">'.$b.'</pre>','<pre class="brush:      python">'.str_replace("<br />", "\n", $b)."</pre>", $string);
                              }
                          }
                      }
      
                      return $string;
      
      }

      Comment

      Working...
      X