Adding a function to an extra line break?

I have a function that sanitizes the input from the form and another function that decodes it. This is similar to bbcode, which also converts the line breaks to <br />

when storing it in the database (using a function nl2br()

) and then converts it <br />

back to line breaks whenever it is put back in the "edit page" field where the user can edit their post (using str_replace('<br />',"\n",$data))

...

The problem is that every time a post is decoded for editing and then re-encoded for storage, each one <br />

turns into two <br /><br />

.

Is \ n or \ r equal to two HTML line breaks?

Here is the code for two functions.

function sanitize2($data) {

  $patterns = array();
    $patterns[0] = '/</';
    $patterns[1] = '/>/';
  $data1 = preg_replace($patterns, "", $data);

  $bopen = substr_count($data1, '[b]') + substr_count($data1, '[B]');
  $bclosed = substr_count($data1, '[/b]') + substr_count($data1, '[/B]');
  $iopen = substr_count($data1, '[i]') + substr_count($data1, '[I]');
  $iclosed = substr_count($data1, '[/i]') + substr_count($data1, '[/I]');
  $uopen = substr_count($data1, '[u]') + substr_count($data1, '[U]');
  $uclosed = substr_count($data1, '[/u]') + substr_count($data1, '[/U]');

    $bx = $bopen - $bclosed;
    $ix = $iopen - $iclosed;
    $ux = $uopen - $uclosed;

    if ($bx > 0) {
        for ($i = 0; $i < $bx; $i++) {
            $data1 .= "[/b]";
        } 
    }
    if ($ix > 0) {
        for ($i = 0; $i < $ix; $i++) {
            $data1 .= "[/i]";
        } 
    }
    if ($ux > 0) {
        for ($i = 0; $i < $ux; $i++) {
            $data1 .= "[/u]";
        } 
    }

  $newer = sanitize($data1);

    $search  = array('[b]', '[/b]', '[i]', '[/i]', '[u]', '[/u]', '[B]', '[/B]', '[I]', '[/I]', '[U]', '[/U]');
    $replace = array('<b>', '</b>', '<i>', '</i>', '<u>', '</u>', '<b>', '</b>', '<i>', '</i>', '<u>', '</u>');

  $newest = str_replace($search, $replace, $newer );

  $final = nl2br($newest);

  return $final;

}

function decode($data) {

$new = str_replace('<br />',"\n",$data);

    $search  = array('[b]', '[/b]', '[i]', '[/i]', '[u]', '[/u]');
    $replace = array('<b>', '</b>', '<i>', '</i>', '<u>', '</u>');

  $newer = str_replace($replace, $search, $new );

  return $newer;
}

      

UPDATE:

I found this page that gives a workaround. Apparently this is a problem with the nl2br () function.: - /

http://websolstore.com/how-to-use-nl2br-and-its-reverse-br2nl-in-php/

+3


source to share


1 answer


From the documentation for nl2br :

nl2br - Insert HTML line breaks before all newline characters in a line

(a main attention).



It doesn't replace newlines, it just inserts rows. So if you try to revert nl2br

by replacing <br />

, you get two \n

, the old one and the one you inserted when you replaced <br />

.

The simplest fix would be to remove everything \n

in the string derived from nl2br

, the correct thing would be to store the text without <br />

and convert when displayed.

+1


source







All Articles