0

My code works as follows:

  • Text comes to server (from textarea)
  • Text is ran through trim() then nl2br

But what is happening is it is adding a <br> but not removing the new line so

"

something"

becomes

"<br>

something"

which adds a double new line. Please help this error is ruining all formatting, I can give more code on request.

Creation of post: Shortened creation method (Only showing relevent bits) Creation method:

BlogPost::Create(ParseStr($_POST['Content']));

ParseStr runs:

return nl2br(trim($Str));

Viewing of post:

echo "<span id='Content'>".BlogPosts::ParseBB(trim($StoredPost->Content))."</span>";

ParseBB runs:

    $AllowedTags = array(
    //  i => Tag, Tag Replacement, Closing tag
        0 => array("code","pre class='prettyprint'",true),
        1 => array("center","span style='text-align:center;'",true),
        2 => array("left","span style='text-align:right;'",true),
        3 => array("right","span style='text-align:left;'",true)
    );
    $AllowedTagsStr = "<p><a><br><br/><b><i><u><img><h1><h2><h3><pre><hr><iframe><code><ul><li>";
    $ParsedStr = $Str;

    foreach($AllowedTags as $Tag)
    {
        $ParsedStr = str_replace("<".$Tag[0].">","<".$Tag[1].">",$ParsedStr);
        if($Tag[2])
            $ParsedStr = str_replace("</".$Tag[0].">","</".$Tag[1].">",$ParsedStr);
    }


    return strip_tags($ParsedStr,$AllowedTagsStr);

Example: What I see: What is shown:

user1763295
  • 860
  • 3
  • 16
  • 34
  • Can you share your PHP that takes the form input and parses it – Casey Flynn Jul 16 '13 at 01:58
  • 1
    We request more code please. – jh314 Jul 16 '13 at 01:58
  • More info added for you guys. – user1763295 Jul 16 '13 at 02:05
  • The additional line should not be visible when output to a browser. Unless wrapped in `
    `, the browser ought to only display it as a single 'new line'.  Is it possible you have styles applied to your `
    ` elements?
    – Jerbot Jul 16 '13 at 02:05
  • Well, the bug only appears on content that is wrapped in pre (Which is what my code happens to be wrapped in) so that would explain it. What can I do to fix this, I can't change it from pre either. – user1763295 Jul 16 '13 at 02:10
  • possible duplicate of [How to remove line breaks (no characters!) from the string?](http://stackoverflow.com/questions/10757671/how-to-remove-line-breaks-no-characters-from-the-string) – kenorb Mar 03 '15 at 00:27

2 Answers2

1

It's because nl2br() doesn't remove new lines at all.

Returns string with <br /> or <br> inserted before all newlines (\r\n, \n\r, \n and \r).

Use str_replace instead:

$string = str_replace(array("\r\n", "\r", "\n"), "<br />", $string);
kenorb
  • 155,785
  • 88
  • 678
  • 743
0

Aren't you using UTF-8 charset? If you are using multibyte character set (ie UTF-8), trim will not work well. You must use multibyte functions. Try something like this one: http://www.php.net/manual/en/ref.mbstring.php#102141

Inside <pre> you should not need to call nl2br function to display break lines.

Check if you really want to call nl2br when you are creating post. You probably need it only on displaying it.

Fanda
  • 3,760
  • 5
  • 37
  • 56