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开发者_StackOverflow 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?
nl2br($article)
http://www.php.net/nl2br
nl2br($string_name, false); // where string name is the name of your variable.
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;
}
}
精彩评论