Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: echo nl2br??

  1. #1
    New Coder
    Join Date
    Apr 2004
    Posts
    49
    Thanks
    0
    Thanked 0 Times in 0 Posts

    echo nl2br??

    I'm trying to use "echo nl2br" to seperate new lines from a DB driven description for a job, but when I added "nl2br" to "echo" it puts a huge space at the top of the page, if I remove "nl2br" from "echo" the page appears correctly,, I not explaining it very well so I'll just show an example:

    with "nl2br" to "echo" the page appears like this:

    --title image


    {
    huge space added
    }



    --then copy

    without "nl2br" to "echo" the page appears like this:

    --title image
    --then copy

    no huge space being added. appears correctly

    why is that? and how can I work around it? or fix it

    Thanks for any direction
    Ken

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think it would be helpful to see some code.

    It might also be that the text that you are passing to nl2br() has a bunch of new line characters in it? Upon executing it, take a look at the HTML source code in your browser and see if you see a bunch of <br/>'s in the area you're describing. If you do, chances are that the text has lots of new line characters in it and that's where the problem lies.

    But, it's hard to debug without seeing the code...

    Sadiq.

  • #3
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,106
    Thanks
    11
    Thanked 101 Times in 99 Posts
    its probably as Sadiq says so maybe try
    PHP Code:
    <?echo nl2br(trim($string));?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #4
    New Coder
    Join Date
    May 2003
    Location
    in a small damp cupboard
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by kred
    why is that? and how can I work around it? or fix it
    looks like extra whitespace at the beginning of the var, try this:

    PHP Code:
    <?php
    // trim -- Strip whitespace from the beginning and end of a string
    // [url]http://www.php.net/trim[/url]
    echo nl2br(trim($foo));
    ?>
    edit: beat me to it!
    might be worth sticking in a regexp to replace more than 2 occurances of whitespace with just 2:
    PHP Code:
    $foo eregi_replace("((\r)?\n){3,}","\r\n\r\n",$foo); 
    that should do it, haven't tested it so don't blame me if it doesn't work
    Last edited by mrjamin; 10-08-2004 at 03:58 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •