Jump to content

Recommended Posts

Basically, I have a text area used to post announcement's on one of my pages, it writes the value to a database, and that's read on all appropriate pages.

 

But my problem came when I wanted to be able to take line breaks in the announcements, after a bit of reading, I discovered that the Pre Tag would allow me to do this, but it doesn't quite do what I need:

<pre><textarea name=content wrap=physical rows=10 cols=40 onkeyup=limiter()></textarea></pre>

 

What that will return, in place of any line breaks is /r/n, I assume it means the same thing, but the browser doesn't read it properly, does anyone know of a way to convert /r/n to a br tag? Or another method entirely to accomplish my goal?

 

Thanks in advance

 

~Glenugie~

Link to comment
https://forums.phpfreaks.com/topic/176954-solved-text-area-with-pre-tags/
Share on other sites

Hi Glenugie,

 

nl2br adds a <br> but leaves the original \n in place so in some scenarios when using hte <pre> tag you can have problems with double-spacing.

 

There is a user-submitted (sean at boyercentral dot net) function on the manual page for the nl2br() function which will help.

 

<?php
// do nl2br except when in a pre tag
function nl2brPre($string)
{
    // First, check for <pre> tag
    if(!strpos($string, "<pre>"))
    {
        return nl2br($string);
    }

    // If there is a <pre>, we have to split by line 
    // and manually replace the linebreaks with <br />
    $strArr=explode("\n", $string);
    $output="";
    $preFound=false;

    // Loop over each line
    foreach($strArr as $line)
    {    // See if the line has a <pre>. If it does, set $preFound to true
        if(strpos($line, "<pre>"))
        {
            $preFound=true;
        }
        elseif(strpos($line, "</pre>"))
        {
            $preFound=false;
        }
        
        // If we are in a pre tag, just give a \n, else a <br />
        if($preFound)
        {
            $output .= $line . "\n";
        }
        else
        {
            $output .= $line . "<br />";
        }
    }

    return $output;
}
?>

 

Hope this helps.

  • 5 years later...

Hello Bricktop,

 

I seen your code which you had mentioned above. Actually I want to copy and paste References in to my database from word file. I am trying to use textarea tag but it is showing like a paragraph so guide me how to do and what to do ?

 

 

 <textarea name="reference" style="width:400px; height:80px;overflow:visible;"></textarea>

 

in database field name is "references".

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.