Development Simple HTML - <Pre>

Discussion in 'Software' started by Agamer, 9 Nov 2005.

  1. Agamer

    Agamer Minimodder

    Joined:
    10 Mar 2005
    Posts:
    437
    Likes Received:
    4
    On my website I have a news system, it fetches it's content from a my sql databse which the users submit to, well thats all working great

    When it comes to displaying the information I want it to retain all the formatting such as line breaks and things like that. So I used pre which worked great, the only problem is when a line of text is inserted which is longer than the box it's meant to be in it keeps going, when it was in a <P> it dropped onto the next line. They have the exact same setup in css.

    So what I want is the formatting of <pre> to allow line breaks and the fitting into the space it's meant to be of <p>

    I know this may be very simple but I havn't thought of a way.

    Thankyou in advance
     
  2. simon w

    simon w What's a Dremel?

    Joined:
    3 Nov 2003
    Posts:
    1,302
    Likes Received:
    0
    Have a look at the nl2br function, it'll convert new lines into <br> tags :)
     
    Last edited: 9 Nov 2005
  3. RTT

    RTT #parp

    Joined:
    12 Mar 2001
    Posts:
    14,120
    Likes Received:
    74
    As simon said, nl2br is what you want as browsers ignore formatting (HTML tags are presentation).

    Here's my 'fixed' version of nl2br which produces proper paragraphs rather than just thousands of <br /> tags.

    PHP:
        function fixed_nl2br($str)
        {
            
    $retval '<p>';
            
            
    $lines explode("\n"$str);
            
            
    $c count($lines);

            for (
    $i 0$i $c$i++)
            {
                if (
    '' == trim($lines[$i]) && != $i)                // empty lines are the start and end of paragraphs
                
    {
                    
    $lines[$i] = '</p><p>'
                }
                else if(
    array_key_exists(($i+1), $lines) && ('' != $lines[$i 1]  && ($i 1) < $c))        // if the next line isnt empty and we're not counting ahead of the amount of lines we have, we can add a break
                
    {
                    
    $lines[$i] .= '<br />';
                }
                
                
    $retval .= $lines[$i];
            }
            
            
    $retval .= '</p>';
            
            return 
    $retval;
            
        }
     
  4. Agamer

    Agamer Minimodder

    Joined:
    10 Mar 2005
    Posts:
    437
    Likes Received:
    4
    Thankyou for all your help, I am yet to implement this but I am sure it will work.

    Thankyou.
     

Share This Page