Duncan Palmer
Duncan Palmer

Reputation: 2913

Automatically insert Line break?

I'm making a news posting system and I was wondering if it is possible to automatically insert a <br /> tag where ever there is a new line?

For example, if posted a news article with the following text in the text area:

New news article

Features
- 1
- 2
- 3

it would add this to the news database:

New news article<br/>
<br/>
Features<br/>
- 1<br/>
- 2<br/>
- 3<br/>

Is there a way to do this?

Upvotes: 1

Views: 2836

Answers (3)

Clarkson
Clarkson

Reputation: 1

nl2br($string_name, false); // where string name is the name of your variable.

Upvotes: 0

Miraage
Miraage

Reputation: 3464

You should replace "\n" character with "br" tag while parsing $_POST data.

For example:

/**
 * Cleans incoming data
 *
 * @param array $data - Incoming data, $_GET or $_POST, for example
 * @return void
 */     
function cleanPost(&$data)
{
    foreach ($data as $k => $v)
    {
        if (!is_array($v))
        {
            // Your filters here
            $v = str_replace("\n", '<br />', $v);
        }
        $data[$k] = $v;
    }
}

Upvotes: -2

deceze
deceze

Reputation: 522005

nl2br($article)

http://www.php.net/nl2br

Upvotes: 7

Related Questions