MyBB Community Forums

Full Version: Help: Custom Mybb Codes Error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello! i used the below code to create a custom mycode for textarea on my forum.
 


$standard_mycode['textarea']['regex'] = "#\[textarea\](.*?)\[/textarea\]#si";

$standard_mycode['textarea']['replacement'] = "<textarea>$1</textarea>";

but the problem is whenever the mycode is used, whatever is inside the textarea has a <br /> tag how can i remove every br tags from the textarea created?
Quote:but the problem is whenever the mycode is used, whatever is inside the textarea has a <br /> tag how can i remove every br tags from the textarea created?
I had the same problem but having created our own codebox mycode instead.

The process i used....
  • had to modify server file /inc/class_parser.php
  • create a new function in the file at the beginning


function my_nl2br($string){
$string = str_replace("\n", "<br />", $string);
if(preg_match_all('/\<textarea\>(.*?)\<\/textarea\>/', $string, $match)){
    foreach($match as $a){
        foreach($a as $b){
        $string = str_replace('<textarea>'.$b.'</textarea>', "<textarea>".str_replace("<br />", "", $b)."</textarea>",         $string);
        }
    }
}
  • find and replace the execution of the original
find this

        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);
        }
and replace with this

        if(!isset($this->options['nl2br']) || $this->options['nl2br'] != 0)
        {
            $message = my_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);
        }

all your changing is the one line
$message = my_nl2br($message);
(2016-10-17, 06:42 PM)metulburr Wrote: [ -> ]
Quote:but the problem is whenever the mycode is used, whatever is inside the textarea has a <br /> tag how can i remove every br tags from the textarea created?
I had the same problem but having created our own codebox mycode instead.

The process i used....
  • had to modify server file /inc/class_parser.php
  • create a new function in the file at the beginning


[code]function my_nl2br($string){
$string = str_replace("\n", "<br />", $string);

It is not working here, can you please check through the codes again?
the code is correct. Is your mycode replacement match the string replacement exactly?


your html insert code must match the replacement for mycode exactly
my class_parser.php function:

function my_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;

}

and my mycode
[attachment=37654]

But because you first replace the mycode with your replacement code, then in class_parser replace that again for newlines, it must exactly match otherwise it wont find that part of the string. So if i removed the class="brush: python" it would not work, or even if i added/removed a space it would also not work.
This is the my_nl2br function i used:


function my_nl2br($string){
    $string = str_replace("\n", "<br />", $string);
    if(preg_match_all('/\<textarea\>(.*?)\<\/textarea\>/', $string, $match)){
        foreach($match as $a){
            foreach($a as $b){
                $string = str_replace('<textarea>'.$b.'</textarea>','<textarea>'.str_replace("<br />", "\n", $b)."</textarea>", $string);
                        }
                    }
                }

                return $string;

}

Then i created the my codes from my mycode settings in Admin Panel.

The textarea worked perfectly but any texts within the textarea usually have br tags.
Quote:The textarea worked perfectly but any texts within the textarea usually have br tags.
Does your mycode replacement have any class definitions? Or something to make it different than the function?
Thank you very much, i have got it to work!


function my_nl2br($string){
    $string = str_replace("\n", "<br />", $string);
    if(preg_match_all('/\<textarea\>(.*?)\<\/textarea\>/', $string, $match)){
        foreach($match as $a){
            foreach($a as $b){
                $string = str_replace('<textarea>'.$b.'</textarea>','<textarea>'.str_replace("<br />", "\n", $b)."</textarea>", $string);
                        }
                    }
                }

                return $string;

}



awesome!!